immutable / inout / pure headaches

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Jul 6 11:28:20 UTC 2018


On Friday, July 06, 2018 11:10:27 Timoses via Digitalmars-d-learn wrote:
> I dared once again getting into immutable by adding an
> "immutable" keyword which causes a chain of actions to be taken.
> I feel like I'm lost in a jungle of immutable, inout and pure
> (perhaps more will join the party...).
>
> To start off, why does this not work?
>
>
>   class Test
>   {
>       private S s;
>       this(S t) { this.s = t; }
>       this(immutable S t) immutable { this.s = t; }
>
>          inout(Test) get() inout
>          {
>              // Error: none of the overloads of __ctor are
> callable using a inout object, candidates are:
> //onlineapp.d(10):        onlineapp.Test.this(S t)
> //onlineapp.d(11):        onlineapp.Test.this(immutable(S) t)
>               return new inout Test(this.s);
>          }
>   }
>
>   struct S
>   {
>       int[] a;
>   }
>   void main()
>   {
>       immutable S s = immutable S([1,2,3]);
>       auto t = new immutable Test(s);
>   }

You have no constructor that will work with inout - only mutable amd
immutable. inout is only going to work when the object is always treated as
either inout or const, because it could be an object that's mutable, const,
or immutable. It can't ever treat it as mutable or immutable within the
function that marks it as inout.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list