cannot implicitly convert expression (array(map(split("a")))) of type string[][] to immutable(string[][])

Yuxuan Shui via Digitalmars-d digitalmars-d at puremagic.com
Sun Jan 29 21:03:31 PST 2017


On Sunday, 29 January 2017 at 07:37:00 UTC, Fangrui Song wrote:
> What cannot `string[][]` be implicitly converted to 
> `immutable(string[][])` as in `not_compilable`?
>
>
>
> import std.stdio, std.range, std.algorithm, std.array;
>
> void main() {
>   immutable not_compilable = "a".split.map!(x => x.split).array;
>   static immutable compilable0 = "a".split.map!(x => 
> x.split).array;
>   immutable compilable1 = "a".split.map!"a.split".array;
> }
>
>
>
> % dmd --version
> DMD64 D Compiler v2.073.0
> Copyright (c) 1999-2016 by Digital Mars written by Walter Bright
>
> % dmd a.d
> a.d(3): Error: cannot implicitly convert expression 
> (array(map(split("a")))) of type string[][] to 
> immutable(string[][])

immutable in D means array elements can't be modified by anyone 
(i.e. they will never change).

You cannot implicitly convert string[][] to immutable(string[][]) 
because the elements may still be modified through the original 
reference, which breaks the guarantee.

Correct way is to use .idup, or assumeUnique.


More information about the Digitalmars-d mailing list