Re: Static if a Function Exists
On Friday, 3 April 2020 at 07:08:03 UTC, WebFreak001 wrote: maybe not the optimal solution because stringof isn't properly defined, but currently I don't think there is a better way than: template matchesTemplateConstraints(alias fn, Args...) { enum def = fn.stringof; // private void testFun(string op, T)(Cls a, T b) if (isNumeric!T) {} mixin("private void testFun" ~ def[def.indexOf('(') .. $] ~ " {}"); enum matchesTemplateConstraints = __traits(compiles, testFun!Args); } void main() { // true pragma(msg, matchesTemplateConstraints!(opBinaryImpl, "+", int)); // false pragma(msg, matchesTemplateConstraints!(opBinaryImpl, "+", string)); } You can also static foreach over __traits(getOverloads, mixin(__MODULE__), "opBinaryImpl", true) if you have multiple templates of same name I will try that out, thanks. I do have overloads, and somehow I missed the existence of `__traits(getOverloads)`. Ah, I see, take the string of the function code (didn't know you could do that...) and create a local duplicate without the body included. Hacky, but a workable solution if D does not have anything better.
Re: Error: module `vibe` is in file 'vibe\vibe.d' which cannot be read
On Friday, 3 April 2020 at 20:49:33 UTC, Steven Schveighoffer wrote: On 4/3/20 4:21 PM, Baby Beaker wrote: [...] That error generally means it can't find the import. Dub should take care of the download of the module, and the import directory. Note, this runs for me on my system. Try doing this from the command line: dub run -v --single thefile.d instead of using the #! directive. This will print all the steps and commands dub uses to build the file, maybe there's a clue there. -Steve Now I get this work, but dub is very slow. Almost 1 minute for compile.
Re: How user dub packages in dmd without dub.exe ?
On Saturday, 4 April 2020 at 20:21:03 UTC, Marcone wrote: I want import modules from dub packages in my program.d and run using dmd.exe without dub.exe. How can I make it? Becouse when I try to import it says that can not found. Ag the end dub is calling DMD/LDC with some arguments. You can run dub with verbose output and check the arguments in your scenario. If you can copy the D packages from the Dub packages into your main source folder, the command maybe is just: dmd -i -run app.d (-i will find the module dependencies without explicitly mentioning them as command line args). Kind regards Andre
How user dub packages in dmd without dub.exe ?
I want import modules from dub packages in my program.d and run using dmd.exe without dub.exe. How can I make it? Becouse when I try to import it says that can not found.
Re: Linear array to matrix
On Saturday, 4 April 2020 at 14:00:01 UTC, 9il wrote: On Saturday, 4 April 2020 at 09:25:14 UTC, Giovanni Di Maria wrote: [...] You may want to look into a mir-algorithm package that supports rectangular multidimensional arrays like NumPy. [...] Very good. Thank you G
Re: No implicit opOpAssign for structs with basic types?
On Saturday, 4 April 2020 at 12:14:23 UTC, Robert M. Münch wrote: On 2020-04-04 10:32:32 +, Ferhat Kurtulmuş said: struct S { float a; float b; S opOpAssign(string op)(ref S rhs) if (op == "+"){ this.a += rhs.a; this.b += rhs.b; return this; } } If the struct is from some 3rd party source, how can I add such an operator overloading to it? Is it possible to "extend" a struct later? Oh I see what you mean now. In JavaScript you can for instance extend Array with an user defined method like; Array.prototype.insert = function(index) { ... return this; }; Of course in d CTFE helps for similar cases. But I am afraid operator overloading outside struct/class body is not available in D. It would be nice feature though.
Re: No implicit opOpAssign for structs with basic types?
On 4/4/20 8:45 AM, Steven Schveighoffer wrote: >> Yes, sure, but in C++ I don't have to explicitly write this down. It >> just works. IMO that makes a lot of sense as long as all types fit. >> This just looks superfluously. >> > > steves@homebuild:~$ cat test.cpp > struct S > { > float a; > float b; > }; > int main() > { > S a = {1, 5}; > S b = {2, 5}; > > a += b; > } > steves@homebuild:~$ g++ -o test test.cpp > test.cpp: In function ‘int main()’: > test.cpp:11:4: error: no match for ‘operator+=’ (operand types are ‘S’ > and ‘S’) >a += b; >~~^~~~ > > Doesn't seem to "just work" for me... I was about to say the same. C++ does not have this feature. What it has as a feature and as a guideline is to define an operator+ outside of the type's definition. Perhaps that's what's helping in C++ in this case: the type looks clean but there are "interface" functions outside of it. I've used the following trick in D for many of my types, which I've been copy-pasting but it can be mixed in: struct S { float a; float b; int opCmp(const(typeof(this)) that) const { import std.typecons : tuple; return tuple(this.tupleof).opCmp(tuple(that.tupleof)); } } unittest { assert(S(3) < S(4)); assert(S(1, 2) > S(1, 1)); } void main() { } Ali
Re: Dynamically init a struct with values calculated from other values in the struct?
On 4/4/20 5:13 AM, Robert M. Münch wrote: I need to dynamically initialize a fixed number of entries of the form: (start, length) and iterate over them. Hence, I think a struct makes sense: struct S { s1, l1 , s2, l2 , s3, l3 } Now I need to initialize the values like this: S myS = {s1:0, l1:10, s2:(2*s1), ...}; So I somehow would like to reference the prior values to initialize the other members. I haven't found a way to do it. Is this possible at all? I don't think so. Or do I just init the struct empty and then set the entries? I recommend using a factory function or ctor. -Steve
Re: No implicit opOpAssign for structs with basic types?
On 4/4/20 8:14 AM, Robert M. Münch wrote: On 2020-04-04 10:32:32 +, Ferhat Kurtulmuş said: struct S { float a; float b; S opOpAssign(string op)(ref S rhs) if (op == "+"){ this.a += rhs.a; this.b += rhs.b; return this; } } If the struct is from some 3rd party source, how can I add such an operator overloading to it? Is it possible to "extend" a struct later? No. operators must be implemented by the type itself. That is intentional (so types always act the same no matter where you import them). The only exception is UFCS, which doesn't cover operator overloading. However, you can make a wrapper and use alias this. -Steve
Re: No implicit opOpAssign for structs with basic types?
On 4/4/20 8:07 AM, Robert M. Münch wrote: On 2020-04-04 10:32:32 +, Ferhat Kurtulmuş said: Probably I didn't understand what you mean. Sorry if this is not the case, but this one is easy. ... struct S { float a; float b; S opOpAssign(string op)(ref S rhs) if (op == "+"){ this.a += rhs.a; this.b += rhs.b; return this; } } void main() { S a = {1, 5}; S b = {2, 5}; a += b; writeln(a); } ... Yes, sure, but in C++ I don't have to explicitly write this down. It just works. IMO that makes a lot of sense as long as all types fit. This just looks superfluously. steves@homebuild:~$ cat test.cpp struct S { float a; float b; }; int main() { S a = {1, 5}; S b = {2, 5}; a += b; } steves@homebuild:~$ g++ -o test test.cpp test.cpp: In function ‘int main()’: test.cpp:11:4: error: no match for ‘operator+=’ (operand types are ‘S’ and ‘S’) a += b; ~~^~~~ Doesn't seem to "just work" for me... -Steve
Vibe-d Error Message and failed with exit code 1
Program.d #!/usr/bin/env dub /+ dub.sdl: name "hello_vibed" dependency "vibe-d" version="~>0.8.0" +/ import vibe.vibe; import std; void main() { writeln("Ola Mundo!"); listenHTTP("127.0.0.1:8080", (req, res) { res.writeBody("Hello Vibe.d: " ~ req.path); }); runApplication(); } In cmd prompt: dub fetch vibe-d dub Program.d I get this Error, How solve it?: C:\Users\Usuario\AppData\Local\dub\packages\vibe-d-0.8.6\vibe-d\http\vibe\http\s erver.d(949,24): Deprecation: function std.typecons.Nullable!(CookieValueMap).Nu llable.get_ is deprecated - Implicit conversion with alias Nullable.get this wil l be removed after 2.096. Please use .get explicitly. C:\Users\Usuario\AppData\Local\dub\packages\vibe-d-0.8.6\vibe-d\http\vibe\http\s erver.d(962,38): Deprecation: function std.typecons.Nullable!(DictionaryList!(st ring, true, 16u, false)).Nullable.get_ is deprecated - Implicit conversion with alias Nullable.get this will be removed after 2.096. Please use .get explicitly. C:\Users\Usuario\AppData\Local\dub\packages\vibe-d-0.8.6\vibe-d\http\vibe\http\s erver.d(1041,18): Deprecation: function std.typecons.Nullable!(DictionaryList!(s tring, true, 16u, false)).Nullable.get_ is deprecated - Implicit conversion with alias Nullable.get this will be removed after 2.096. Please use .get explicitly . D:\dmd2\windows\bin\..\..\src\phobos\std\range\primitives.d(174,38): Deprecation : alias byKeyValue this is deprecated - Iterate over .byKeyValue instead. D:\dmd2\windows\bin\..\..\src\phobos\std\range\primitives.d(176,27): Deprecation : alias byKeyValue this is deprecated - Iterate over .byKeyValue instead. D:\dmd2\windows\bin\..\..\src\phobos\std\range\primitives.d(174,38): Deprecation : alias byKeyValue this is deprecated - Iterate over .byKeyValue instead. D:\dmd2\windows\bin\..\..\src\phobos\std\range\primitives.d(176,27): Deprecation : alias byKeyValue this is deprecated - Iterate over .byKeyValue instead. D:\dmd2\windows\bin\..\..\src\phobos\std\range\primitives.d(174,38): Deprecation : alias byKeyValue this is deprecated - Iterate over .byKeyValue instead. D:\dmd2\windows\bin\..\..\src\phobos\std\range\primitives.d(176,27): Deprecation : alias byKeyValue this is deprecated - Iterate over .byKeyValue instead. D:\dmd2\windows\bin\..\..\src\phobos\std\range\primitives.d(174,38): Deprecation : alias byKeyValue this is deprecated - Iterate over .byKeyValue instead. D:\dmd2\windows\bin\..\..\src\phobos\std\range\primitives.d(176,27): Deprecation : alias byKeyValue this is deprecated - Iterate over .byKeyValue instead. D:\dmd2\windows\bin\..\..\src\phobos\std\range\primitives.d(174,38): Deprecation : alias byKeyValue this is deprecated - Iterate over .byKeyValue instead. D:\dmd2\windows\bin\..\..\src\phobos\std\range\primitives.d(176,27): Deprecation : alias byKeyValue this is deprecated - Iterate over .byKeyValue instead. D:\dmd2\windows\bin\..\..\src\phobos\std\range\primitives.d(174,38): Deprecation : alias byKeyValue this is deprecated - Iterate over .byKeyValue instead. D:\dmd2\windows\bin\..\..\src\phobos\std\range\primitives.d(176,27): Deprecation : alias byKeyValue this is deprecated - Iterate over .byKeyValue instead. D:\dmd2\windows\bin\..\..\src\phobos\std\range\primitives.d(174,38): Deprecation : alias byKeyValue this is deprecated - Iterate over .byKeyValue instead. D:\dmd2\windows\bin\..\..\src\phobos\std\range\primitives.d(176,27): Deprecation : alias byKeyValue this is deprecated - Iterate over .byKeyValue instead. D:\dmd2\windows\bin\..\..\src\phobos\std\range\primitives.d(174,38): Deprecation : alias byKeyValue this is deprecated - Iterate over .byKeyValue instead. D:\dmd2\windows\bin\..\..\src\phobos\std\range\primitives.d(176,27): Deprecation : alias byKeyValue this is deprecated - Iterate over .byKeyValue instead. lld-link: error: C:\Users\Usuario\AppData\Local\dub\packages\eventcore-0.8.50\ev entcore\lib\ws2_32.lib: unknown file type lld-link: error: C:\Users\Usuario\AppData\Local\dub\packages\eventcore-0.8.50\ev entcore\lib\kernel32.lib: unknown file type lld-link: error: C:\Users\Usuario\AppData\Local\dub\packages\vibe-d-0.8.6\vibe-d \lib\win-i386\libssl.lib: unknown file type lld-link: error: C:\Users\Usuario\AppData\Local\dub\packages\vibe-d-0.8.6\vibe-d \lib\win-i386\libcrypto.lib: unknown file type Error: linker exited with status 1 D:\dmd2\windows\bin\dmd.exe failed with exit code 1. fgf
Re: No implicit opOpAssign for structs with basic types?
On Saturday, April 4, 2020 4:22:29 AM MDT Robert M. Münch via Digitalmars-d- learn wrote: > D doesn't have implicit operators for structs? > > struct S {float a, float b}; > S a = {1, 5}; > S b = {2, 5); > > a += b; > Error: a is not a scalar, it is a S > > So I really have to write an overloaded operator for such cases? You could just use the constructor syntax. e.g. S a = S(1, 5); S b = S(2, 5); - Jonathan M Davis
Re: Linear array to matrix
On Saturday, 4 April 2020 at 09:25:14 UTC, Giovanni Di Maria wrote: Hi. Is there a Built-in function (no code, only a built-in function) that transform a linear array to a Matrix? For example: From [10,20,30,40,50,60,70,80,90,100,110,120]; To [ [10,20,30], [40,50,60], [70,80,90], [100,110,120] ]; Thank You very much Cheers. Giovanni You may want to look into a mir-algorithm package that supports rectangular multidimensional arrays like NumPy. /+dub.sdl: dependency "mir-algorithm" version="~>3.7.27" +/ // http://mir-algorithm.libmir.org/mir_ndslice.html import mir.ndslice; void main() { // auto intArray = [10,20,30,40,50,60,70,80,90,100,110,120]; auto intMatrix = intArray.sliced(4, 3); static assert(is(typeof(intMatrix) == Slice!(int*, 2))); // lazy matrix auto lazyMatrix = iota!int([4, 3]/*shape*/, 10/*start*/, 10/*stride*/); assert(intMatrix == lazyMatrix); //or foreach(i; 0 .. intMatrix.length) foreach(j; 0 .. intMatrix.length!1) assert(intMatrix[i, j] == lazyMatrix[i, j]); }
Re: No implicit opOpAssign for structs with basic types?
On 2020-04-04 10:32:32 +, Ferhat Kurtulmuş said: struct S { float a; float b; S opOpAssign(string op)(ref S rhs) if (op == "+"){ this.a += rhs.a; this.b += rhs.b; return this; } } If the struct is from some 3rd party source, how can I add such an operator overloading to it? Is it possible to "extend" a struct later? -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Re: No implicit opOpAssign for structs with basic types?
On 2020-04-04 10:32:32 +, Ferhat Kurtulmuş said: Probably I didn't understand what you mean. Sorry if this is not the case, but this one is easy. ... struct S { float a; float b; S opOpAssign(string op)(ref S rhs) if (op == "+"){ this.a += rhs.a; this.b += rhs.b; return this; } } void main() { S a = {1, 5}; S b = {2, 5}; a += b; writeln(a); } ... Yes, sure, but in C++ I don't have to explicitly write this down. It just works. IMO that makes a lot of sense as long as all types fit. This just looks superfluously. -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Re: CT BitArray
On Friday, 3 April 2020 at 20:06:50 UTC, Steven Schveighoffer wrote: On 4/3/20 3:13 PM, Johan wrote: On Thursday, 2 April 2020 at 12:41:28 UTC, Steven Schveighoffer wrote: Hm... I thought there was precedent for providing fallback implementations for intrinsics. That is, you define the function, which is only used if the intrinsic is not available. I can't remember where I saw this. But you could try this by simply implementing the bitops in core.bitop, and see if they are used outside ctfe. There are a bunch of functions implemented with `if (!__ctfe)`. DMD and LDC are smart enough to elide `if(false/true)` control flow completely even in debug code, so there is no penalty to using `if (!__ctfe)`. See for example: https://github.com/ldc-developers/druntime/blob/ldc/src/core/bitop.d#L85 Nice! I'm trying to understand that. It looks like you are calling the intrinsic that llvm recognizes. What can you do when bsf *is* the intrinsic? Does DMD have to change its intrinsic to something that's not core.bitop.bsf so we can call it? Or would that code as written work if ported to druntime mainline? I think it'd work without any changes needed. (note the version statements) I.e. the compiler replaces the bsf with the intrinsic and ignores the implementation in runtime code, but works at comiple time. I think that's correct, yes. -Johan
Re: Linear array to matrix
On Saturday, 4 April 2020 at 10:52:30 UTC, MoonlightSentinel wrote: On Saturday, 4 April 2020 at 09:25:14 UTC, Giovanni Di Maria wrote: Is there a Built-in function (no code, only a built-in function) that transform a linear array to a Matrix? You can combine slide [1] and array [2]: import std; void main() { auto input = [10,20,30,40,50,60,70,80,90,100,110,120]; auto output = input.slide(3, 3).array; writeln(input); writeln(); writeln(output); } Note that output is a view of input and not a copy. [1] https://dlang.org/phobos/std_range.html#.slide [2] https://dlang.org/phobos/std_array.html#.array Thank you very much. Giovanni
Re: Linear array to matrix
On Saturday, 4 April 2020 at 10:52:00 UTC, Boris Carvajal wrote: On Saturday, 4 April 2020 at 09:25:14 UTC, Giovanni Di Maria wrote: Hi. Is there a Built-in function (no code, only a built-in function) that transform a linear array to a Matrix? For example: From [10,20,30,40,50,60,70,80,90,100,110,120]; To [ [10,20,30], [40,50,60], [70,80,90], [100,110,120] ]; Thank You very much Cheers. Giovanni If you're really sure about the array and matrix dimensions/types. You can use a cast: int[] a = [10,20,30,40,50,60,70,80,90,100,110,120]; int[3][] m1 = cast(int[3][]) a; writeln(m1); A better way is using the function chunks; import std.range; auto m2 = a.chunks(3); writeln(m2); Ok. Thank you for your help Giovanni
Re: Simple array problem
On Saturday, 4 April 2020 at 10:57:36 UTC, MoonlightSentinel wrote: On Saturday, 4 April 2020 at 09:09:44 UTC, Giovanni Di Maria wrote: Why the followin code gives me the error? Error: only one index allowed to index `int[3][3]` Use matrix[2][2] instead of matrix[2,2]. OK. Thank you very MUCH Giovanni
Re: Simple array problem
On Saturday, 4 April 2020 at 09:09:44 UTC, Giovanni Di Maria wrote: Why the followin code gives me the error? Error: only one index allowed to index `int[3][3]` Use matrix[2][2] instead of matrix[2,2].
Re: Linear array to matrix
On Saturday, 4 April 2020 at 09:25:14 UTC, Giovanni Di Maria wrote: Is there a Built-in function (no code, only a built-in function) that transform a linear array to a Matrix? You can combine slide [1] and array [2]: import std; void main() { auto input = [10,20,30,40,50,60,70,80,90,100,110,120]; auto output = input.slide(3, 3).array; writeln(input); writeln(); writeln(output); } Note that output is a view of input and not a copy. [1] https://dlang.org/phobos/std_range.html#.slide [2] https://dlang.org/phobos/std_array.html#.array
Re: Linear array to matrix
On Saturday, 4 April 2020 at 09:25:14 UTC, Giovanni Di Maria wrote: Hi. Is there a Built-in function (no code, only a built-in function) that transform a linear array to a Matrix? For example: From [10,20,30,40,50,60,70,80,90,100,110,120]; To [ [10,20,30], [40,50,60], [70,80,90], [100,110,120] ]; Thank You very much Cheers. Giovanni If you're really sure about the array and matrix dimensions/types. You can use a cast: int[] a = [10,20,30,40,50,60,70,80,90,100,110,120]; int[3][] m1 = cast(int[3][]) a; writeln(m1); A better way is using the function chunks; import std.range; auto m2 = a.chunks(3); writeln(m2);
Re: No implicit opOpAssign for structs with basic types?
On Saturday, 4 April 2020 at 10:22:29 UTC, Robert M. Münch wrote: D doesn't have implicit operators for structs? struct S {float a, float b}; S a = {1, 5}; S b = {2, 5); a += b; Error: a is not a scalar, it is a S So I really have to write an overloaded operator for such cases? Probably I didn't understand what you mean. Sorry if this is not the case, but this one is easy. ... struct S { float a; float b; S opOpAssign(string op)(ref S rhs) if (op == "+"){ this.a += rhs.a; this.b += rhs.b; return this; } } void main() { S a = {1, 5}; S b = {2, 5}; a += b; writeln(a); } ...
No implicit opOpAssign for structs with basic types?
D doesn't have implicit operators for structs? struct S {float a, float b}; S a = {1, 5}; S b = {2, 5); a += b; Error: a is not a scalar, it is a S So I really have to write an overloaded operator for such cases? -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Linear array to matrix
Hi. Is there a Built-in function (no code, only a built-in function) that transform a linear array to a Matrix? For example: From [10,20,30,40,50,60,70,80,90,100,110,120]; To [ [10,20,30], [40,50,60], [70,80,90], [100,110,120] ]; Thank You very much Cheers. Giovanni
Dynamically init a struct with values calculated from other values in the struct?
I need to dynamically initialize a fixed number of entries of the form: (start, length) and iterate over them. Hence, I think a struct makes sense: struct S { s1, l1 , s2, l2 , s3, l3 } Now I need to initialize the values like this: S myS = {s1:0, l1:10, s2:(2*s1), ...}; So I somehow would like to reference the prior values to initialize the other members. I haven't found a way to do it. Is this possible at all? Or do I just init the struct empty and then set the entries? -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Simple array problem
Hi. I am very new to D Language and I apologize for my bad question. Why the followin code gives me the error? Error: only one index allowed to index `int[3][3]` import std.stdio; import std.array; void main() { int[3][3] matrix; matrix[2,2]=99; } Thank you very much GIovanni