Comment #0 by bearophile_hugs — 2010-07-16T05:21:15Z
A helper function for Rebindable!() can help remove some noise from the code, this is a first try:
import std.typecons, std.traits;
template isImmutable(T) {
enum bool isImmutable = is(const(T) == T) || is(immutable(T) == T);
}
Rebindable!T rebindable(T)(T obj)
if ((is(T == class) || is(T == interface) || isArray!T) && isImmutable!T) {
return Rebindable!T(obj);
}
// usage example
import std.stdio;
class Foo {}
void main() {
auto a = rebindable(new immutable(Foo));
assert(a.sizeof == 4);
a = new immutable(Foo);
}
I don't know if isImmutable!() works well in all cases, I have not tested it much. But a template like isImmutable can be useful in Phobos.
Comment #1 by bearophile_hugs — 2010-08-15T10:05:41Z