Function literal bug?

bearophile bearophileHUGS at lycos.com
Thu Nov 28 00:23:20 PST 2013


Sergei Nosov:

> 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.

Global structs don't need the "static" attribute.

This version of your code gives the output 6 6 on Windows 32 bit:


import std.stdio;

T identity(T)(T e) { return e; }

struct S(alias Func) {
     void call() {
         Func("string").length.writeln;
     }
}

struct S1 {
     alias A1 = S!identity;
     //alias A2 = S!(x => x);
     alias A3 = S!(function string(string s) => s);
}

void main() {
     S1.A1.init.call;
     S1.A3.init.call;
}


I don't know why in A2 it infers a delegate.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list