Implicit conversion to mutable if no indirections?

Dmitry Olshansky dmitry.olsh at gmail.com
Sat Sep 3 19:36:28 UTC 2022


On Friday, 2 September 2022 at 18:58:43 UTC, Ali Çehreli wrote:
> An issue I have is non-mutable expressions without indirections 
> not converting to mutable. I talked about this before and 
> mentioned Unqual at DConf 2022 to explain how it solves this.
>
> The problem happens in template code:
>
> void main() {
>   const i = 42;  // const, so foo's T will be const below
>   foo(i);
> }
>
> void foo(T)(T value) {
>   T result;
>   ++result;  // Compilation error; but should it work?
> }
>
> I feel like it should work because 'result' is a local int.

My take on this is that passing by value is equivalent of shallow 
(1 level deep) unqual. This is sadly not representable in the 
current language.

struct A {
     int[] slice;
}

void main() {
      const a = A([1, 2, 3]);
      pass(a);
      // still have const a here
}

void pass(Unqual!(const A) value) {
     // value is really this:
     // struct A { const(int)[]slice; }
     value.slice ~= 4; // should work
}

—
Dmitry Olshansky




More information about the Digitalmars-d mailing list