Re: Hipreme's #1 Tip of the day
On Wednesday, 19 October 2022 at 23:28:46 UTC, Hipreme wrote: Hey guys, I'm going to start making a tip of the day (although I'm pretty sure I won't be able to give every day a tip), but those things are really interesting to newcomers to know and may be obvious to some of the old schoolers there. Always public import a type that the user (including you) is expected to interact with: Imagine we have a module A that define a tree ```d module a; struct Tree(T){} ``` If you create a module b containing a function that returns a tree: ```d module b; import a; Tree!string getDirectoryTree(string entry){return null;} ``` This is virtually unusable! One must `public import a: Tree;` This will make your API a lot easier to interact with, keep in mind to always public import some type that is used from another dependency like this, but try to not overdo it. Will add an important tip for when handling variables of integer type smaller than `int` : ```D ushort a, b, c; c = a + b;//expression a + b will be promoted to int or compiler will emit a deprecation warning c = cast(ushort) (a + b);//works fine c = cast(typeof(c)) (a + b);//alternate form c = cast(ushort) a + b;//bad, only "a" will be casted so you get promoted to int/warned anyway c += a;//assigning operators' operands won't be promoted, with same types at least auto d = a + b;//will probably be of type int ``` note: i didn't 101% check but the above holds true in my usage at least.
Hipreme's #1 Tip of the day
Hey guys, I'm going to start making a tip of the day (although I'm pretty sure I won't be able to give every day a tip), but those things are really interesting to newcomers to know and may be obvious to some of the old schoolers there. Always public import a type that the user (including you) is expected to interact with: Imagine we have a module A that define a tree ```d module a; struct Tree(T){} ``` If you create a module b containing a function that returns a tree: ```d module b; import a; Tree!string getDirectoryTree(string entry){return null;} ``` This is virtually unusable! One must `public import a: Tree;` This will make your API a lot easier to interact with, keep in mind to always public import some type that is used from another dependency like this, but try to not overdo it.
Re: Find out what type my class is being converted to for comparisons
On Tuesday, 18 October 2022 at 18:53:41 UTC, Matthew Rushworth wrote: I am in the process of building a matrix class (uni project, with my choice of programming language) and appear to have run into a problem that I'm not too sure how to fix. My class uses templates to define the shape of the matrix, although I'm not sure if that matters. As part of my class, I included a opEquals, but when I try to assert that a matrix created by multiplying with the identity matrix matches the output (I checked with liberal writeln()s to make sure they do) but my opEquals() function (I moved it out of the class as I kept getting errors saying it was supposed to be non-const, and couldn't figure out how to fix that either) just isn't called. --- Matrix code (the only bit of the class used by opEquals): ``` class Matrix(size_t X, size_t Y = X){ const size_t x_length = X; const size_t y_length = Y; double[X*Y] data; ... } ``` The entirety of opEquals: ``` /// both matrices have to have an identical shape to compare /// each element must be identical to match bool opEquals(size_t X, size_t Y)(const Matrix!(X,Y) m1, const Matrix!(X,Y) m2) { for (int i = 0; i < m1.data.length; ++i) if (m1.data[i] != m2.data[i]) return false; return true; } ``` and the code that excepts: ``` void main() { auto m = new Matrix!(2)(); m.data = [1.0,0.0,0.0,1.0].dup; auto n = new Matrix!(2)(); n.data = [2.0,3.0,4.0,5.0].dup; auto u = mult(m,n); writeln("u.data vs n.data:"); for (int i = 0; i < u.data.length; ++i) writeln(u.data[i], "\t", n.data[i]); // using opEquals() directly is working, but it doesn't seem to be being used //assert(opEquals(u,n),"\"opEquals(u, n)\" is failing."); // this works fine assert(u == n,"\"u == n\" is failing."); // this fails. Why? } ``` --- Any help would be very much appreciated. A way to figure out what the '==' is converting its arguments to would also be great, but I'm not sure it exists (beyond just sitting down and trying to work it out myself, of course) What's the reason for it being a class? struct makes more sense here, plus it'll be faster If it were a struct, you'd not need any of that, and you can also have operator overloading for your mult while also being able to check how many row/col at compile time, here an example: ```D import std; struct Matrix(size_t X, size_t Y = X) { enum x_length = X; // enum so it's available only at compile time enum y_length = Y; double[X * Y] data; // operator overloading Matrix opBinary(string op)(Matrix rhs) { static if (op == "*") { // // ** ADD YOUR MULT OPERATION HERE ** // static if (X == 2) { return Matrix(); } else static assert("not supported"); } else static assert(0, "Operator " ~ op ~ " not implemented"); } } void main() { // check equality by value: auto a = Matrix!(2)(); a.data = 0; auto b = Matrix!(2)(); b.data = 0; writeln("are they the same?: ", a == b); // it'll check for equality by value auto m = Matrix!(2)(); m.data = [1.0, 0.0, 0.0, 1.0]; auto n = Matrix!(2)(); n.data = [2.0, 3.0, 4.0, 5.0]; // here we can use the * operator! auto u = m * n; writeln("u.data vs n.data:"); for (int i = 0; i < u.data.length; ++i) writeln(u.data[i], "\t", n.data[i]); } ```
Re: Catching C errors
On Wednesday, 19 October 2022 at 16:47:49 UTC, data pulverizer wrote: On Wednesday, 19 October 2022 at 14:05:35 UTC, data pulverizer wrote: Hi all, I am calling code from a C API, and would like to know how to catch exit errors so that they can be handled and make them more like an exceptions so that the computation can continue. I've tried something similar to what is outlined here: https://dlang.org/phobos/object.html#.Error but it doesn't work, the process dies in the try block. Thanks It's okay, I've found a work around. Always good to share what it is, for future readers who have the same question
Compiler Error while using Validation in the hunt-framework
Hello, I started playing with D and the hunt-framework. But I bumped into an error using Validation. ```d module app.forms.LoginForm; import hunt.validation; import hunt.framework.http.Form; class LoginForm : Form { mixin MakeForm; @Email string name; @Length(3,8) string password; } ``` The error looks like that: `../hunt/validation/DeclDef.d-mixin-41(54,108): Error: undefined identifier arg` When I remove the parameters from `@Length`, it compiles. I tried the `dicoth` example application, but I get the same error for the validation used there. I use this compiler: DMD64 D Compiler v2.100.2 under Linux. My current D knowledge is not sufficient to fix the bug. No idea how the annotation and mixin templates work together here. I didn't find any documentation, how to write custom annotations. Can somebody give me a hint? BR Roman
Re: Catching C errors
On 10/19/22 09:47, data pulverizer wrote: > It's okay, I've found a work around. Do you simply check the return value? Ali
Re: Catching C errors
On 10/19/22 07:05, data pulverizer wrote: > I am calling code from a C API, and would like to know how to catch exit > errors If you are talking about the exit() Posix function, you can't do anything about that because its purpose is to cause "normal process termination". Ali
Re: Catching C errors
On Wednesday, 19 October 2022 at 14:05:35 UTC, data pulverizer wrote: Hi all, I am calling code from a C API, and would like to know how to catch exit errors so that they can be handled and make them more like an exceptions so that the computation can continue. I've tried something similar to what is outlined here: https://dlang.org/phobos/object.html#.Error but it doesn't work, the process dies in the try block. Thanks It's okay, I've found a work around.
Catching C errors
Hi all, I am calling code from a C API, and would like to know how to catch exit errors so that they can be handled and make them more like an exceptions so that the computation can continue. I've tried something similar to what is outlined here: https://dlang.org/phobos/object.html#.Error but it doesn't work, the process dies in the try block. Thanks
Re: Find out what type my class is being converted to for comparisons
On 10/19/22 00:43, Matthew Rushworth via Digitalmars-d-learn wrote: Thank you, that worked perfectly, not sure exactly what I did wrong, I'm assuming I forgot to make the parameter a const variable Object, the root of the class object hierarchy already defines an opEquals [1] and you need to overload/overwrite that for it to work properly. Only class methods are considered, hence ag0aep6g's suggestion. [1] https://dlang.org/library/object/object.op_equals.html