digitalmars.D.learn - Strange error
- Jack Applegame (24/24) Mar 21 2021 Could someone please explain what is wrong with this code?
- Imperatorn (2/26) Mar 21 2021 https://forum.dlang.org/thread/mggbomxhjpglltsihqek@forum.dlang.org
- Jack Applegame (2/33) Mar 21 2021 There are no non-global templates in my snippet.
- Steven Schveighoffer (18/47) Mar 21 2021 You have the wrong lines attributed with the error message. The error
- MichaelJames (2/26) Mar 22 2021 Tell me, did you manage to solve this problem?
- Nicholas Wilson (2/3) Mar 23 2021 https://github.com/dlang/dmd/pull/12300
Could someone please explain what is wrong with this code? https://glot.io/snippets/fwxn2198kv ```d import std.stdio; struct Sample{ void function() func1; void function() func2; } void noth(Sample smpl)() { smpl.func1(); // Error: expression __lambda1 is not a valid template value argument smpl.func2(); // Error: expression __lambda2 is not a valid template value argument } void main(){ enum s = Sample( {writeln("Hello world1");}, {writeln("Hello world2");} ); s.func1(); s.func2(); noth!(s)(); } ```
Mar 21 2021
On Sunday, 21 March 2021 at 07:18:10 UTC, Jack Applegame wrote:Could someone please explain what is wrong with this code? https://glot.io/snippets/fwxn2198kv ```d import std.stdio; struct Sample{ void function() func1; void function() func2; } void noth(Sample smpl)() { smpl.func1(); // Error: expression __lambda1 is not a valid template value argument smpl.func2(); // Error: expression __lambda2 is not a valid template value argument } void main(){ enum s = Sample( {writeln("Hello world1");}, {writeln("Hello world2");} ); s.func1(); s.func2(); noth!(s)(); } ```https://forum.dlang.org/thread/mggbomxhjpglltsihqek forum.dlang.org
Mar 21 2021
On Sunday, 21 March 2021 at 08:45:19 UTC, Imperatorn wrote:On Sunday, 21 March 2021 at 07:18:10 UTC, Jack Applegame wrote:There are no non-global templates in my snippet.Could someone please explain what is wrong with this code? https://glot.io/snippets/fwxn2198kv ```d import std.stdio; struct Sample{ void function() func1; void function() func2; } void noth(Sample smpl)() { smpl.func1(); // Error: expression __lambda1 is not a valid template value argument smpl.func2(); // Error: expression __lambda2 is not a valid template value argument } void main(){ enum s = Sample( {writeln("Hello world1");}, {writeln("Hello world2");} ); s.func1(); s.func2(); noth!(s)(); } ```https://forum.dlang.org/thread/mggbomxhjpglltsihqek forum.dlang.org
Mar 21 2021
On 3/21/21 3:18 AM, Jack Applegame wrote:Could someone please explain what is wrong with this code? https://glot.io/snippets/fwxn2198kv ```d import std.stdio; struct Sample{ void function() func1; void function() func2; } void noth(Sample smpl)() { smpl.func1(); // Error: expression __lambda1 is not a valid template value argument smpl.func2(); // Error: expression __lambda2 is not a valid template value argument } void main(){ enum s = Sample( {writeln("Hello world1");}, {writeln("Hello world2");} ); s.func1(); s.func2(); noth!(s)(); } ```You have the wrong lines attributed with the error message. The error message is in the assignment of s (line 15 and 16) Essentially, you can't use lambda functions as compile-time values. Valid template value parameters are [1] "any type which can be statically initialized at compile time. Template value arguments can be integer values, floating point values, nulls, string values, array literals of template value arguments, associative array literals of template value arguments, or struct literals of template value arguments." Now, one might argue that it's possible to statically initialize function pointers at compile time (i.e. you can do this as a static initialized function pointer at module scope), so possibly this is something that can be fixed. There might already be a bug report on it, or you can file one. It might end up with the spec being clarified to explicitly cover this case. -Steve [1] https://dlang.org/spec/template.html#template_value_parameter
Mar 21 2021
On Sunday, 21 March 2021 at 07:18:10 UTC, Jack Applegame wrote:Could someone please explain what is wrong with this code? https://glot.io/snippets/fwxn2198kv ```d import std.stdio; struct Sample{ void function() func1; void function() func2; } void noth(Sample smpl)() { smpl.func1(); // Error: expression __lambda1 is not a valid template value argument smpl.func2(); // Error: expression __lambda2 is not a valid template value argument } void main(){ enum s = Sample( {writeln("Hello world1");}, {writeln("Hello world2");} ); s.func1(); s.func2(); noth!(s)(); } ```Tell me, did you manage to solve this problem?
Mar 22 2021
On Monday, 22 March 2021 at 07:52:14 UTC, MichaelJames wrote:Tell me, did you manage to solve this problem?https://github.com/dlang/dmd/pull/12300
Mar 23 2021