www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: partially mutable immutable type problem, crazy idea

reply Yuxuan Shui <yshuiv7 gmail.com> writes:
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
May 08 2018
parent reply jmh530 <john.michael.hall gmail.com> writes:
On Tuesday, 8 May 2018 at 22:31:10 UTC, Yuxuan Shui wrote:
 snip]
This doesn't compile for me on run.dlang.io: onlineapp.d(22): Error: template onlineapp.f cannot deduce function from argument types !()(B), candidates are: onlineapp.d(1): onlineapp.f(T)(immutable T a)
May 08 2018
parent Yuxuan Shui <yshuiv7 gmail.com> writes:
On Wednesday, 9 May 2018 at 00:58:51 UTC, jmh530 wrote:
 On Tuesday, 8 May 2018 at 22:31:10 UTC, Yuxuan Shui wrote:
 snip]
This doesn't compile for me on run.dlang.io: onlineapp.d(22): Error: template onlineapp.f cannot deduce function from argument types !()(B), candidates are: onlineapp.d(1): onlineapp.f(T)(immutable T a)
Not supposed to. Was proposing an (crazy) idea here.
May 08 2018