The following code: pragma(msg, typeof(x => x)); prints out void. Semi understandable, if unintuitive. this also does test(alias fn, T)(T t) if(is(typeof(fn(t)))) { pragma(msg, typeof(fn)); } test!(x => x < 5)(10); Which is more annoying. There should either be an error in this case Also happens with expanded function(...){...} syntax, as long as the parameter types are not specified.
A lambda expression that needs parameter type inference is internally translated to template function. x => x // auto __lambda(T1)(T1 x){ return x; } So this: pragma(msg, typeof(x => x)); is same as: pragma(msg, typeof(__lambda)); // template itself has no type, then typeof(template) always returns void And, long years, typeof(template) returns void. Therefore, typeof(x => x) == void is current implementation design. I don't know why typeof(template) returns void, but changing the behavior will break some existing meta-programming codes.
(In reply to comment #1) > And, long years, typeof(template) returns void. > Therefore, typeof(x => x) == void is current implementation design. > > I don't know why typeof(template) returns void, but changing the behavior will > break some existing meta-programming codes. I think it's acceptable to break such meta-programming code. I think pragma(msg, typeof(tem)); should show something useful when tem is a template function (like the name of the template if it has a name, or a lambda template plus line number if it has no name). There are people working on an algorithm to automatically synthesize good names for anonymous functions, but probably it's overkill in D "Naming Anonymous JavaScript Functions": http://blog.getfirebug.com/2011/04/28/naming-anonymous-javascript-functions/ http://code.google.com/p/querypoint-debugging/downloads/detail?name=NamingJSFunctions.pdf
There is another case where using void as the type of template lambdas causes problems, (but of course I can't remember where). It might be worth adding a Ttemplate dummy type and just printing "template" here.
Related: https://issues.dlang.org/show_bug.cgi?id=15437 I briefly talked to both Andrei and Walter today at dconf about this. They both agreed, that calling typeof on a template should be an error (similiar to how calling typeof on a type is an error). Any thoughts on that?
This is documented now: "If a function literal is aliased, the inference of the parameter types is done when the types are needed, as the function literal becomes a template." https://dlang.org/spec/expression.html#lambda-parameter-inference "If the expression is a Template, typeof gives the type void." https://dlang.org/spec/type.html#typeof