www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - type of functions

reply Alain De Vos <devosalain ymail.com> writes:
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.
May 10 2021
parent Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Monday, 10 May 2021 at 10:37:51 UTC, Alain De Vos wrote:
 Can I say that the compiler is intelligent enough to add the 
 attributes "pure nothrow  nogc  safe" ?
Yes, in those cases (and some others). The functionality is described here: https://dlang.org/spec/function.html#function-attribute-inference -- Simen
May 10 2021