type of functions

Alain De Vos devosalain at ymail.com
Mon May 10 10:37:51 UTC 2021


I've written a small program and also it's output,
```
import std.stdio;

double myfun(int x) pure nothrow @nogc @safe{
	return x/10.0;
}

void main()
{
static double Gfunction(int x)
     {return x/10.0;}
writeln(typeid(typeof(Gfunction)));
//OUTPUT:"double function(int) pure nothrow @nogc @safe"
double Lfunction(int x)
     {return x/10.0;}
writeln(typeid(typeof(Lfunction)));
//OUTPUT:"double function(int) pure nothrow @nogc @safe"

alias gfunction = double function(int);
gfunction g = & Gfunction;
writeln(typeid(typeof(g)));
//OUTPUT: "double function(int)*"
double function(int) F = function double(int x)
     {return x/10.0;};
writeln(typeid(typeof(F)));
//OUTPUT: "double function(int)*"

alias lfunction = double delegate(int);
lfunction l = & Lfunction;
writeln(typeid(typeof(l)));
//OUTPUT:"double delegate(int)"
int c=2;
double delegate(int)   D = delegate double(int x)
     {return c*x/10.0;};
writeln(typeid(typeof(D)));
//OUTPUT:"double delegate(int)"
}

```

Can I say that the compiler is intelligent enough to add the 
attributes
"pure nothrow @nogc @safe" ? As far as I understand it means I do 
not need much and I will not change much.

What I find suprising is that "function" is a pointer and 
"delegate" is not as reported type .

Feel free to elaborate.




More information about the Digitalmars-d-learn mailing list