Discussion Thread: DIP 1042--ProtoObject--Community Review Round 1
Adam D Ruppe
destructionator at gmail.com
Sat Jan 15 13:19:10 UTC 2022
On Saturday, 15 January 2022 at 09:49:43 UTC, Alexandru Ermicioi
wrote:
> That was an example ofc. Equals method could be overloaded with
> const and immutable version in there too, or have separate
> iface.
So just like today...
>> Note that opEquals like this already works on dmd master with
>> the specific class and attributes.
>
> From what I'm aware, with couple of compiler hacks, while this
> offers no hacks.
No, there's no compiler hacks, this is a direct consequence of
the Liskov substitution principle allowing you to tighten
constraints in specializations.
How do you think it works today?
> Anyway, if there is no possibility to make friends inheritance
> and method attributes easily, then best is to just remove them
> from Object and be done with it.
Not only is there a possibility, it *already works*.
The DIP authors just don't know very much about D's classes.
> P.S. Can't we enhance compiler and bake the attribute info into
> interface itself, and then allow downcasting it to an interface
> with specific subset of attribtues?
> I.e. say we have safe nothrow nogc equals interface. We then in
> some random code could downcast safely to safe nogc equals
> interface?
Have you tried this?
```
interface GcEqual(T) {
bool opEquals(T rhs);
}
interface NoGcEqual(T) {
bool opEquals(T rhs) @nogc;
}
class A : GcEqual!A, NoGcEqual!A {
override bool opEquals(Object rhs) {
return this.opEquals(cast(A) rhs);
}
override bool opEquals(A rhs) @nogc {
if(rhs is null) return false;
return true;
}
}
void main() {
A a = new A;
GcEqual!A g = a;
NoGcEqual!A n = a;
}
```
Works today. Restricted functions can implicitly cast to less
restricted functions.
More information about the Digitalmars-d
mailing list