Thread overview
alias & local function
May 10, 2021
Alain De Vos
May 10, 2021
Basile B.
May 10, 2021
Alain De Vos
May 10, 2021
Alain De Vos
May 10, 2021
Alain De Vos
May 10, 2021

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 10, 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 -> delegate
>
  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)

add static in the second case.

May 10, 2021

I wonder why ?

May 10, 2021

I see.
Just like in Java.

May 10, 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));
}