Deprecating this(this)

jmh530 john.michael.hall at gmail.com
Tue Apr 3 19:27:09 UTC 2018


On Saturday, 31 March 2018 at 23:38:06 UTC, Andrei Alexandrescu 
wrote:
> [snip]
>
> * immutable and const are very difficult, but we have an attack 
> (assuming copy construction gets taken care of)
>

Would it be easier if the const/immutable containers were 
considered separate types? For instance, in the code below, there 
is InoutFoo and then Foo takes InoutFoo as an alias this (you 
could do the same thing with immutable, but then you’d have to 
include two get functions). This would be like inheriting the 
InoutFoo.

With better syntax, InoutFoo would be something like inout(Foo) 
and the compiler could recognize that the mutable constructor is 
also defined and to call that when appropriate.

struct InoutFoo
{
     int a;

     this(int b) inout
     {
         this.a = b;
     }

     int get() inout
     {
         return a;
     }
}

struct Foo
{
     InoutFoo inoutfoo;
     alias inoutfoo this;

     this(int b)
     {
         a = b;
     }

     void set(int b)
     {
         a = b;
     }
}


void main()
{
     auto x = immutable InoutFoo(1);
     auto y = Foo(1);

     assert(is(typeof(y) : typeof(x)));

     //x.a++; //not allowed
     y.a++;

     assert(x.a == 1);
     assert(y.a == 2);

     assert(x.get == 1);
     y.set(3);
     assert(y.get == 3);
}



More information about the Digitalmars-d mailing list