Overzealous immutable and classes
bearophile
bearophileHUGS at lycos.com
Thu Jul 15 19:43:43 PDT 2010
Jonathan M Davis:
> It's not terribly pretty, but apparently no one could come up with a
> satistfactory way of doing it in the language itself given the syntax for
> references. So, Rebindable!(T) is the solution.
A helper function can help:
import std.stdio, std.typecons, std.traits;
template isImmutable(T) {
// I don't know if this works well in all cases
const 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);
}
class Foo { int x, y; }
void main() {
auto a = rebindable(new immutable(Foo(1, 2)));
assert(a.sizeof == 4);
a = new immutable(Foo(3, 4));
}
A template like isImmutable can be useful in Phobos...
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list