Thread overview | |||||
---|---|---|---|---|---|
|
March 25, 2016 Initializing global delegate variable - bug or on purpose? | ||||
---|---|---|---|---|
| ||||
int delegate(int) dg = (i) => i * 2; Error: non-constant nested delegate literal expression __lambda3 int delegate(int) dg; static this() { dg = i => i * 2; // ok } Am I doing anything wrong? Atila |
March 25, 2016 Re: Initializing global delegate variable - bug or on purpose? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Atila Neves | On Friday, 25 March 2016 at 20:54:28 UTC, Atila Neves wrote:
> int delegate(int) dg = (i) => i * 2;
>
> Error: non-constant nested delegate literal expression __lambda3
>
>
> int delegate(int) dg;
>
> static this() {
> dg = i => i * 2; // ok
> }
>
>
> Am I doing anything wrong?
>
> Atila
Hmm, looks like your first delegate is a function type and the second is a function instance. So the first version written like this ...
import std.stdio;
alias dg = int delegate(int);
dg make_dg(){
return i => i*2;
}
void main(){
auto my_dg = make_dg();
writeln(my_dg(3));
}
will work.
|
March 26, 2016 Re: Initializing global delegate variable - bug or on purpose? | ||||
---|---|---|---|---|
| ||||
Posted in reply to data pulverizer | On Friday, 25 March 2016 at 23:40:37 UTC, data pulverizer wrote:
> On Friday, 25 March 2016 at 20:54:28 UTC, Atila Neves wrote:
>> int delegate(int) dg = (i) => i * 2;
>>
>> Error: non-constant nested delegate literal expression __lambda3
>>
>>
>> int delegate(int) dg;
>>
>> static this() {
>> dg = i => i * 2; // ok
>> }
>>
>>
>> Am I doing anything wrong?
>>
>> Atila
>
> Hmm, looks like your first delegate is a function type and the second is a function instance. So the first version written like this ...
>
> import std.stdio;
>
> alias dg = int delegate(int);
>
> dg make_dg(){
> return i => i*2;
> }
>
> void main(){
> auto my_dg = make_dg();
> writeln(my_dg(3));
> }
>
> will work.
In fact for the second case I'd probably need to see a working struct/class prototype to make a firm comment on it.
|
Copyright © 1999-2021 by the D Language Foundation