www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Strange error

reply Jack Applegame <japplegame gmail.com> writes:
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
next sibling parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
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
parent Jack Applegame <japplegame gmail.com> writes:
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:
 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
There are no non-global templates in my snippet.
Mar 21
prev sibling next sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
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
prev sibling parent reply MichaelJames <MichaelJamesuN63 yahoo.com> writes:
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
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
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