static immutable and lambdas inside struct or class. Is that bug or not?

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 14 08:53:21 PDT 2016


On 08/14/2016 04:27 PM, Uranuz wrote:
> Greatings!
> I need help with these lines bellow. I don't understand why it doesn't
> compile. Is it bug somewhere in Phobos or compiler? Or just I wrote smth
> wrong?
> //---------
> struct A
> {
>     import std.algorithm: map;
>     import std.array: array;
>     import std.typecons: tuple;
>
>
>     static immutable aaa = [
>         tuple("1", "one"),
>         tuple("2", "two"),
>         tuple("3", "three")
>     ];
>
>     static immutable bbb = aaa.map!( a => a[0] ).array;
>
> }
>
> void main()
> {
>     A a = A();
> }
>
> //---------
>
> Compilation output:
> /opt/compilers/dmd2/include/std/algorithm/iteration.d(455): Error:
> this.__lambda6 has no value
>
> You could test it here: https://dpaste.dzfl.pl/67a8cda8f2a8

Looks like a compiler bug, since it works without the struct:

----
import std.algorithm: map;
import std.array: array;
import std.typecons: tuple;


immutable aaa = [
	tuple("1", "one"),
	tuple("2", "two"),
	tuple("3", "three")
];

immutable bbb = aaa.map!( a => a[0] ).array;
----

And that's essentially the same thing.

It has already been filed:
https://issues.dlang.org/show_bug.cgi?id=15908

For a workaround, it works when you explicitly state the type of the 
parameter:

----
struct A
{
     import std.algorithm: map;
     import std.array: array;
     import std.typecons: tuple, Tuple;


     static immutable aaa = [
         tuple("1", "one"),
         tuple("2", "two"),
         tuple("3", "three")
     ];

     alias Tup = Tuple!(string, string);
     static immutable bbb = aaa.map!( (Tup a) => a[0] ).array;
}
----


More information about the Digitalmars-d-learn mailing list