proposal-first-class-protocols icon indicating copy to clipboard operation
proposal-first-class-protocols copied to clipboard

Implementation selection

Open TitanNano opened this issue 4 years ago • 4 comments

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()?

TitanNano avatar Feb 04 '21 15:02 TitanNano

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 avatar Feb 04 '21 15:02 ljharb

@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?

TitanNano avatar Feb 04 '21 19:02 TitanNano

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 avatar Feb 04 '21 19:02 ljharb

@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.

TitanNano avatar Feb 04 '21 19:02 TitanNano