D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7947 - typeof on anonymous function literal returns void
Summary: typeof on anonymous function literal returns void
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2012-04-20 02:19 UTC by James Miller
Modified: 2023-03-24 13:42 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description James Miller 2012-04-20 02:19:04 UTC
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 Kenji Hara 2012-04-20 02:47:58 UTC
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 bearophile_hugs 2012-04-20 04:39:34 UTC
(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 yebblies 2012-11-16 05:21:17 UTC
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 Johannes Loher 2018-05-05 22:44:26 UTC
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?
Comment 5 Nick Treleaven 2023-03-24 13:42:57 UTC
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