Implicit conversion of concatenation result to immutable

Ali Çehreli acehreli at yahoo.com
Thu Apr 1 23:14:35 UTC 2021


On 4/1/21 3:55 PM, H. S. Teoh wrote:
> On Thu, Apr 01, 2021 at 06:34:04PM -0400, Steven Schveighoffer via Digitalmars-d wrote:
> [...]
>> ```d
>> auto concat(T, U)(T[] x, U[] y) pure
>> {
>>      return x ~ y;
>> }
>>
>> void main()
>> {
>>      string x;
>>      const(char)[] y;
>>      string z = concat(x, y); // compiles
>> }
>> ```
> [...]
> 
> Put this way, the solution becomes obvious: `~` should be considered a
> pure operation.  Then the compiler (in theory) ought to be able to
> infer uniqueness from `x ~ y`, and consequently allow implicit
> conversion to immutable.
> 
> 
> T
> 

I admit I've been neglecting indirections. If we are dealing with 
const(S)[], S being a user defined type with indirections, then 
concatenation cannot be S[] because the original S object would not 
allow mutation through them.

This is still within compiler's attribute inference, right? On the other 
hand, this probably would complicate template code: I can imagine a 
template code is tested with simple types and works but fails as soon as 
used with a const(S)[] type at a customer site.

Ali


More information about the Digitalmars-d mailing list