immutable class can't override opEquals, workaround?
SimonN via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Feb 20 20:25:59 PST 2016
Hi,
immutable class A {
int i;
this(int arg) { i = arg; }
override bool opEquals(Object rhsObj)
{
auto rhs = cast (immutable(A)) rhsObj;
return rhs && i == rhs.i;
}
}
Error by dmd 2.070:
./immutclass.d(4): Error: function immutclass.A.opEquals
does not override any function, did you mean to override
'object.Object.opEquals'?
My understandings:
1. immutable class A means: All methods have immutable tacked
on them implicitly.
2. Object.opEquals doesn't have immutable tacked on it. If I
want to override Object.opEquals, I should override without
'immutable'.
3. Overriding opEquals and toHash are necessary to make A
behave properly as AA key type. This is incompatible with
(2) in an immutable class.
4. I found this thread:
How to turn an inout(Object) into a string
http://forum.dlang.org/thread/dcobmtogyrmnaqnqyvbz@forum.dlang.org
that I interpret as: The need for the currently-impossible
override is acknowledged, but the implementation would bring
significant changes to the language, therefore the solution
is postponed. The above thread was from mid-2015, but I guess
it's still relevant.
My workaround is: Make class _A private, and declare every method
immutable, except for what Object decrees to be mutable/const/...
Then make a public alias A = immutable(_A).
Is there something better than this?
Has there been any new development on Object method removal?
Jonathan M Davis has been pushing this hard 2 years ago, I'd
love to see the efforts make it into the language. :-)
-- Simon
More information about the Digitalmars-d-learn
mailing list