tc39 / proposal-first-class-protocols

a proposal to bring protocol-based interfaces to ECMAScript users
352 stars 9 forks source link

Inheritance of symbols from parent to child #23

Open jamiebuilds opened 7 years ago

jamiebuilds commented 7 years ago

I have some questions about this example:

protocol A { a; }
protocol B extends A { b; }

class C implements B {
  [A.a]() {}
  [B.b]() {}
}

In this example, implementing B seems to require referencing A. I was wondering if it made sense to copy over the properties of A to B:

protocol A { a; }
protocol B extends A { b; }

class C implements B {
  [B.a]() {} // copy A.a to B.a
  [B.b]() {}
}

If not, does that mean that B can also have it's own symbol named a?

protocol A { a; }
protocol B extends A { a; b; }

class C implements B {
  [A.a]() {}
  [B.a]() {}
  [B.b]() {}
}

Or should that be an early error?

michaelficarra commented 7 years ago

B can have its own symbol named a. Relatedly, it can also implement A.a for you (that will probably be a common pattern). I don't think any of this is an issue. I'll leave this open for a bit to see if I've missed anything.

jamiebuilds commented 7 years ago

But without redeclaring anything would the child protocol copy the symbols from the parent?

It seems natural to assume that this works because of the extends keyword:

protocol A { a; }
protocol B extends A { b; }

class C implements B {
  [B.a]() {} // copy A.a to B.a
  [B.b]() {}
}
michaelficarra commented 7 years ago

@thejameskyle It would not copy the symbols. I can see how someone might make that assumption, but the relationship implied between B and A by extends is that, in order to implement B, one must also implement A. Would another keyword or symbol make this more clear?

jamiebuilds commented 7 years ago

I think another keyword would be a lot clearer. A synonym of "requires" rather than something that implies inheritance would be best I think

ljharb commented 7 years ago

I think I would be surprised if the keyword was "extends" and it did not set up a [[Prototype]] chain.

michaelficarra commented 7 years ago
protocol A { a; }
protocol B depends on A { b; }

or, to align with #24

protocol A { a; }
protocol B requires A { b; }
Qard commented 7 years ago

👍 for requires.