Function literal bug?

Sergei Nosov sergei.nosov at gmail.com
Wed Nov 27 22:40:11 PST 2013


Hi!

This is kind of bug report/question.

I'm running Ubuntu 12.04 (x64), DMD v2.064.2 and have the 
following code:

T identity(T)(T e) { return e; }
struct S(alias Func)
{
     void call()
     {
         import std.stdio;
         writeln(Func("string").length);
     }
}
static struct S1
{
     alias S!(identity) A1;
     //alias S!(x => x) A2;
     alias S!(function string (string e) { return e; }) A3;
}
void main()
{
     S1.A1.init.call();
     S1.A3.init.call();
}

The main complaint is that function literal is somehow broken in 
that case. The output of the program is
6
4527264
For some reason, length in the second case is wrong.

Another question (which I believe is reasonable) is that I cannot 
use the (x => x) syntax for function literal (the commented alias 
A2). The compilation error is:

tmp.d(15): Error: delegate tmp.S1.__lambda4!string.__lambda4 
function literals cannot be class members
tmp.d(8): Error: template instance tmp.S1.__lambda4!string error 
instantiating
tmp.d(15):        instantiated from here: S!((x) => x)
tmp.d(8): Error: this for __lambda4 needs to be type S1 not type 
S!((x) => x)

I assume, compiler infers the type of the literal to be delegate, 
but it's clearly not the case, since I don't need no context here.

I appreciate any comments.


More information about the Digitalmars-d-learn mailing list