Bug 7947 – typeof on anonymous function literal returns void
Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-04-20T02:19:04Z
Last change time
2023-03-24T13:42:57Z
Keywords
accepts-invalid
Assigned to
No Owner
Creator
James Miller
Comments
Comment #0 by james — 2012-04-20T02:19:04Z
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.
Comment #1 by k.hara.pg — 2012-04-20T02:47:58Z
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.
Comment #2 by bearophile_hugs — 2012-04-20T04:39:34Z
(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
Comment #3 by yebblies — 2012-11-16T05:21:17Z
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.
Comment #4 by johannes.loher — 2018-05-05T22:44:26Z
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?