partially mutable immutable type problem, crazy idea

Yuxuan Shui yshuiv7 at gmail.com
Tue May 8 22:31:10 UTC 2018


After watching the DConf 2018 video, I came up with this wild 
idea:

auto f(T)(immutable T a) {
     // If T is a aggregate type, and I only use (directly
     // or indirectly) the immutable fields of T,
     // Then it should be OK to call f() with a partially mutable 
type
     return a.x+1;
}

void main() {
     struct A {
         int x;
     }
     A a;
     immutable(A) b;
     f(a); // <- not fine
     f(b); // <- fine

     class B {
         immutable int x = 10;
         double f;
     }
     auto c = new B;
     f(c); // <- fine too
}

I think this should solve the reference counting an immutable 
object, no? To f(), T will just looks like a normal immutable, 
uncopyable (because copying means modifying the reference 
counter) type


More information about the Digitalmars-d mailing list