std.array.array and immutable elements

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Nov 7 08:38:27 PST 2014


On Friday, 7 November 2014 at 14:33:00 UTC, Jack Applegame wrote:
> DMD64 D Compiler v2.066.1
> Why second call doesn't compile?
>
> import std.array;
> import std.algorithm;
>
> class Foo {
> 	bool flag;
> }
> void main() {
>   immutable(Foo)[] foos;
>   foreach(i; 0..5) foos ~= new Foo;
> 	
>   // compiles, typeof(bar1) == immutable(Foo)[]
>   auto bar1 = array(foos.filter!(i => i.flag));
> 	
>   // fails, expected typeof(bar2) == immutable(Foo)[]
>   auto bar2 = array(foos.map!(i => i));
> }
>
> Error: cannot implicitly convert expression (arg) of type 
> immutable(Foo) to test.Foo
> Error: template instance 
> std.conv.emplaceRef!(immutable(Foo)).emplaceRef!(immutable(Foo)) 
> error instantiating
> instantiated from here: array!(MapResult!(__lambda2, 
> immutable(Foo)[]))

I'm pretty sure that should work. Please file a bug report.

In the mean-time, either pre-allocate and std.algorithm.copy or - 
if you don't have the length - use std.array.appender and ~= the 
entire range in a single shot.


More information about the Digitalmars-d-learn mailing list