Delegate / Error: cannot implicitly convert expression...

Alex sascha.orlov at gmail.com
Sat Jun 15 16:39:38 UTC 2019


On Saturday, 15 June 2019 at 16:34:22 UTC, Robert M. Münch wrote:
> On 2019-06-15 16:19:23 +0000, Anonymouse said:
>
>> By design, I think: "delegate and function objects cannot be 
>> mixed. But the standard function std.functional.toDelegate 
>> converts a function to a delegate."
>> 
>> Your example compiles if the assignment is changed to dg = 
>> toDelegate(&myFunc); (given appropriate imports).
>> 
>> https://tour.dlang.org/tour/en/basics/delegates
>> 
>> https://dlang.org/phobos/std_functional.html#.toDelegate
>
> Hmm... but this here compiles:
>
> void main()
> {
>    import std.stdio: write, writeln, writef, writefln;
>    void foo(int a) {return; }
>
>    void test()
>    {
>    	void delegate(int) dg;
>
>        dg = &foo;
>    }
> }
>
> See: https://run.dlang.io/is/U7uhAX
>
> Is it because inside main() there is a stack frame? And with a 
> global function there is none? I'm a bit confused...

Correct. Inside main, foo is a delegate.
https://dlang.org/spec/function.html#nested


More information about the Digitalmars-d-learn mailing list