Thread overview
Strange error
Mar 21, 2021
Jack Applegame
Mar 21, 2021
Imperatorn
Mar 21, 2021
Jack Applegame
Mar 22, 2021
MichaelJames
Mar 24, 2021
Nicholas Wilson
March 21, 2021
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)();
}
```


March 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
March 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:
>> 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.
March 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
March 22, 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?
March 24, 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