-
Notifications
You must be signed in to change notification settings - Fork 222
Closed
Description
In the extension struct proposal (#2360) I initially propose to disallow extension structs to extend other extension structs. #2368 discusses whether to allow this. If we allow this, the question then arises of whether or not to allow overriding. This is discussed briefly in this addendum to the proposal. A concrete concern with allowing overriding, is that the semantics for extension structs members is non-virtual dispatch (think extension methods), and hence override behavior can be surprising:
extension struct EvenInteger(int x) {
bool get isEven => true;
}
// Truly odd integers.
extension struct OddInteger(int x) extends EvenInteger {
bool get isEven => false;
}
void test() {
OddInteger i = OddInteger(2);
assert(!i.isEven); // Dispatch goes to OddInteger.isEven
EvenInteger e = i; // Ok
assert(i.isEven); // Dispatch goes to EvenInteger.isEven
}
While this is surprising, it also seems like a large restriction to forbid API authors from modifying the implementation when extending. This issue is for discussion of this design choice.