proposal-first-class-protocols
proposal-first-class-protocols copied to clipboard
Implementation selection
I read the proposal a couple of times now, but I'm still failing to see who implementation selection is going to work.
Let's say I create my own ToString protocol:
Protocol ToString {
symbol;
toString() {
return this[ToString.symbol];
}
}
const myObject = {
[ToString.symbol]: 'myObject',
};
console.log(myObject.toString());
How is the implementation of .toString() being selected? Will this run Object.prototype.toString() or ToString.toString()?
In your case, myObject doesn’t fully implement the protocol, so it’ll fall back to the Object.prototype method. If it did fully implement it, it’d use the ToString.toString method.
@ljharb you are right, I forgot
Protocol.implements(myObject, ToString);
But what if I have two competing Protocols which both implement toString()? And how would I select the original Object.prototype.toString() implementation?
Generally speaking, there'd only be two options: to have the last one win, or to throw. I assume that rather than silently doing the wrong thing, this proposal would choose to throw, so that you couldn't mix competing protocols.
If a protocol you want to use provides a toString implementation, then either you use its toString, or you don't use that protocol.
@ljharb I see, so a library can not use its own protocol internally without affecting the consuming application...
After I opened this, I saw PR #32 which appears to propose using myObject[ToString].toString(), that would avoid this issue, but also feels clunky.