digitalmars.D.learn - alias & local function
- Alain De Vos (29/29) May 09 2021 The following program runs correctly
- Basile B. (3/20) May 09 2021 add static in the second case.
- Alain De Vos (1/1) May 09 2021 I wonder why ?
- Alain De Vos (2/2) May 09 2021 I see.
- Alain De Vos (15/15) May 09 2021 This one runs fine,
The following program runs correctly ``` import std.stdio; int afunction(int x){return x;}; void main() { alias myint = int; myint i=5; alias tfunction = int function(int); tfunction f = & afunction; writeln(f(1)); } ``` This does not: ``` import std.stdio; void main() { int afunction(int x){return x;}; alias myint = int; myint i=5; alias tfunction = int function(int); tfunction f = & afunction; writeln(f(1)); } ``` It gives compile error : Error: cannot implicitly convert expression &afunction of type int delegate(int x) pure nothrow nogc safe to int function(int)
May 09 2021
On Monday, 10 May 2021 at 01:25:10 UTC, Alain De Vos wrote:This does not: ``` import std.stdio; void main() { int afunction(int x){return x;};it's not static so -> context -> delegatealias myint = int; myint i=5; alias tfunction = int function(int); tfunction f = & afunction; writeln(f(1)); } ``` It gives compile error : Error: cannot implicitly convert expression &afunction of type int delegate(int x) pure nothrow nogc safe to int function(int)add static in the second case.
May 09 2021
This one runs fine, ``` import std.stdio; void main() { static int Gfunction(int x){return x;} int Lfunction(int x){return x;} alias gfunction = int function(int); alias lfunction = int delegate(int); gfunction g = & Gfunction; lfunction l = & Lfunction; writeln(g(1)); writeln(l(1)); } ```
May 09 2021