Implicit conversion of unique chars[] to string

ag0aep6g anonymous at example.com
Tue Mar 23 06:16:05 UTC 2021


On 23.03.21 02:07, Steven Schveighoffer wrote:
> const(char)[] x = "foo";
> string chained = chainPath(x, "bar").array;
> 
> Error: cannot implicitly convert expression array(chainPath(x, "bar")) 
> of type const(char)[] to string
> 
> And the answer is complex. You can't accept a const range, because they 
> don't work. The only way to have purity infer uniqueness is to accept 
> paramters that the result could not have come from. Usually this means 
> accepting const and returning mutable.

Ah, right. Purity was a red herring then. If you put a `const(char)[]` 
in and you get a `const(char)[]` out, then the compiler must assume that 
it might be the same one.

We could possibly change `.array` to return a `char[]`. Uniqueness would 
still fail when you pass a `char[]` in, but that could be worked around 
by adding a const temporary.


More information about the Digitalmars-d-learn mailing list