Implicit conversion of unique chars[] to string

Steven Schveighoffer schveiguy at gmail.com
Tue Mar 23 01:07:15 UTC 2021


On 3/22/21 5:58 PM, ag0aep6g wrote:
> On 22.03.21 21:38, Per Nordlöw wrote:
>> Am I the only one being annoyed by the fact that
>>
>>      chainPath(...).array
>>
>> doesn't implicit convert to string despite the array returned from 
>> .array is allocated by the GC.
> 
> Works for me:
> 
> ----
> import std.array: array;
> import std.path: chainPath;
> void main()
> {
>      string chained = chainPath("foo", "bar").array;
> }
> ----
> 
> Uniqueness is being inferred based on purity. If it doesn't work for 
> you, then you're probably doing something impure.

He didn't specify clearly on the original post. Yours works because 
everything is a string.

Try

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.

-Steve


More information about the Digitalmars-d-learn mailing list