Thread overview
[Issue 7947] New: typeof on anonymous function literal returns void
Apr 20, 2012
James Miller
Apr 20, 2012
Kenji Hara
Nov 16, 2012
yebblies
April 20, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7947

           Summary: typeof on anonymous function literal returns void
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: james@aatch.net


--- Comment #0 from James Miller <james@aatch.net> 2012-04-20 02:19:04 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 20, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7947



--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> 2012-04-20 02:47:58 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 20, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7947


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #2 from bearophile_hugs@eml.cc 2012-04-20 04:39:34 PDT ---
(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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 16, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7947


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yebblies@gmail.com


--- Comment #3 from yebblies <yebblies@gmail.com> 2012-11-17 00:21:17 EST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------