std.array.array and immutable elements

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Nov 7 06:57:44 PST 2014


On Fri, 07 Nov 2014 14:32:57 +0000
Jack Applegame via Digitalmars-d-learn
<digitalmars-d-learn at puremagic.com> wrote:

> 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));
> }
`map` cannot return range with immutable elements, 'cause they are
obviously generated by program, and therefore aren't "immutable".

i.e. map function can do alot of things which breaks immutability
promise for range elements, and filter function cannot (as it doesn't
return new element value).

but map can return 'const' ranges, so changing your `foos` to 'const'
will work.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20141107/1f1926ba/attachment.sig>


More information about the Digitalmars-d-learn mailing list