Delegate / Error: cannot implicitly convert expression...

Anonymouse zorael at gmail.com
Sat Jun 15 16:19:23 UTC 2019


On Saturday, 15 June 2019 at 15:54:00 UTC, Robert M. Münch wrote:
> Why does the follwing code give: Error: cannot implicitly 
> convert expression & myFunc of type void function(int a) to 
> void delegate(int)
>
>
> void myFunc(int a){return;}
>
> void main()
> {
>    void delegate(int) dg;
>    dg = &myFunc;
> }
>
> See: https://run.dlang.io/is/iTYo2L

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


More information about the Digitalmars-d-learn mailing list