Unfortunately it is not possible to write this

import std.typecons;
class Info{...}
rebindable!Info x;

I get the following error message

source/app.d(11,3): Error: template std.typecons.rebindable matches more than one template declaration: /usr/include/dmd/phobos/std/typecons.d(1675,14): rebindable(T)(T obj) if (is(T == class) || is(T == interface) || isDynamicArray!T || isAssociativeArray!T)
and
/usr/include/dmd/phobos/std/typecons.d(1694,14): rebindable(T)(Rebindable!T obj)


This would have been equivalent to define a mutable reference to an object of type Info and allow me to write this

alias rebindable!Info InfoR;
InfoR x;

Unfortunately I didn't manage to get something compiling with rebindable. I don't understand how I'm supposed to use it.


Note that I'm designing a library that I would like intuitive to use. Hacking around the problem won't cut it.

I need a type defining a mutable reference to an immutable object. Rebindable doesn't give me that apparently.

I need something allowing me to write this

interface Info {...}
class MyInfos {
    static class Obj : Info {...}
    static immutable Obj one = new immutable Obj(...);
}

mutableObjectRef!Info x1, x2;
assert(x1 is null);
assert(x1 == null);
x1 = MyInfos.one;
assert(x1 is MyInfos.one);
assert(x1 == MyInfos.one);
x2 = x1;

switch(x1){
case MyInfos.one: ... ;
default:...;
}

x1 must have the semantic of an object reference, support polymorphism, allowing to down or up cast and of course access immutable members and methods. MyInfos.one is an object reference that I shouldn't be allowed to modify. I can't use a function because it needs the value semantic so I can use it as a case argument in a switch.

It doesn't seam that this is what rebind provides. It looks like mutableObjectRef should be a templated struct. But I'm not experienced enough in D to implement it. I don't know if its only possible.






Reply via email to