January 29, 2017 cannot implicitly convert expression (array(map(split("a")))) of type string[][] to immutable(string[][]) | ||||
---|---|---|---|---|
| ||||
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[][]) |
January 30, 2017 Re: cannot implicitly convert expression (array(map(split("a")))) of type string[][] to immutable(string[][]) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Fangrui Song | 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.
|
Copyright © 1999-2021 by the D Language Foundation