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

Inheritance of symbols from parent to child

Open jamiebuilds opened this issue 8 years ago • 7 comments

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?

jamiebuilds avatar Oct 02 '17 13:10 jamiebuilds

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.

michaelficarra avatar Oct 02 '17 16:10 michaelficarra

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]() {}
}

jamiebuilds avatar Oct 03 '17 01:10 jamiebuilds

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

michaelficarra avatar Oct 03 '17 01:10 michaelficarra

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

jamiebuilds avatar Oct 03 '17 01:10 jamiebuilds

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

ljharb avatar Oct 03 '17 02:10 ljharb

protocol A { a; }
protocol B depends on A { b; }

or, to align with #24

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

michaelficarra avatar Oct 03 '17 03:10 michaelficarra

👍 for requires.

Qard avatar Nov 08 '17 04:11 Qard