Implicit conversion of unique chars[] to string

Per Nordlöw per.nordlow at gmail.com
Tue Mar 23 10:57:28 UTC 2021


On Tuesday, 23 March 2021 at 01:07:15 UTC, Steven Schveighoffer 
wrote:
> const(char)[] x = "foo";
> string chained = chainPath(x, "bar").array;

that calls the template overload

ForeachType!Range[] array(Range)(Range r)
if (isIterable!Range && !isAutodecodableString!Range && 
!isInfinite!Range)

should be able to implicitly convert to string because the .array 
expression is inferred `pure`. Or is the compiler pessimistically 
assuming that the slice returned from the .array call may reside 
from a reference reachable from the range parameter `r`?

See for instance

@safe pure unittest
{
     import std.path : chainPath;
     import std.array : array;
     const(char)[] x1 = "foo";
     const string x2 = "bar";
     auto y1 = chainPath(x1, x2).array;
     pragma(msg, __FILE__, "(", __LINE__, ",1): Debug: ", 
typeof(y1));
     auto y2 = chainPath(x2, x1).array;
     pragma(msg, __FILE__, "(", __LINE__, ",1): Debug: ", 
typeof(y2));
}

printing

/home/per/f.d(8,1): Debug: const(char)[]
/home/per/f.d(10,1): Debug: const(char)[]


More information about the Digitalmars-d-learn mailing list