How to get an inout constructor working with a template wrapper

aliak something at something.com
Sun Jul 22 22:51:58 UTC 2018


Hi,

In the code below:

struct W(T) {
     T val;
     this(T val) inout {
         this.val = val;
     }
}

class C {}

void main() {
    W!C a = new C;
    immutable W!C b = new C;
}

W!C a = new C results in: "Error: cannot implicitly convert 
expression val of type C to inout(C)."

If I remove the inout on the constructor then the error is on the 
other line and is: "Error: mutable method W!(C).W.this is not 
callable using a immutable object"

If the class is changed to a struct through,  then the 
constructor with inout works on both lines in main above.

So I guess this has something to do with reference types (As the 
same behaviour is exhibited if T == int*) What's the recommended 
way to handle this?

Cheers,
- Ali


More information about the Digitalmars-d-learn mailing list