typeof function literals which define the types of its parameters but do not give their parameters names

Steven Schveighoffer schveiguy at gmail.com
Thu Dec 27 04:27:03 UTC 2018


On 12/26/18 10:52 PM, Johannes Loher wrote:
> Hey all,
> 
> I am a bit confused about the inferred types of function literals which 
> do not name their parameters (something like `(int) {}`). The confusion 
> arises from the fact that the inferred type sometimes is `void` (might 
> be the case, because the function literal is inferred to be a template) 
> and sometimes it is something like `void function(bool _param_0) pure 
> nothrow @nogc @safe`. What is really weir is that seems to depend on the 
> type of the parameter. Here is small example showing what happens for 
> different parameter types: https://run.dlang.io/is/xSMZZu
> 
> Also note that this only happens for function literals. For regular 
> functions and member functions, `void` is never inferred, but only types 
> like `pure nothrow @nogc @safe void(MyClass _param_0)`.
> 
> Has anybody any idea what is going on here? Is this a bug?

This is pretty funny actually.

Note the difference between the ones that print something other than 
"void" is that the parameters are all keywords!

The ones that do print "void" have a normal symbol name as their 
parameter. This could be a parameter name or a parameter type, and the 
compiler is choosing the latter.

In other words, you are expecting:

alias f = (string) {}

to evaluate to something like:

void f(string) {}

but in fact, it evaluates to:

void f(T)(T string) {}

because string is not a keyword, it's an alias. So you are free to 
redefine it inside your lambda (in this case, as a template parameter 
*name*).

As proof, you can try calling it like this:

f(1)

and it works.

To fix, just give all your lambda parameters names.

-Steve


More information about the Digitalmars-d-learn mailing list