www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - static immutable and lambdas inside struct or class. Is that bug or

reply Uranuz <neuranuz gmail.com> writes:
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
Aug 14 2016
parent reply ag0aep6g <anonymous example.com> writes:
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; } ----
Aug 14 2016
parent Uranuz <neuranuz gmail.com> writes:
On Sunday, 14 August 2016 at 15:53:21 UTC, ag0aep6g wrote:
 On 08/14/2016 04:27 PM, Uranuz wrote:
 [...]
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; } ----
OK. I just declared alias for predicate outside struct to workaround. Maybe compiler trying to get context when attemting to infer arguments for some reason, but fails. Or something else...
Aug 14 2016