babel / proposals

✍️ Tracking the status of Babel's implementation of TC39 proposals (may be out of date)
https://github.com/tc39/proposals
432 stars 39 forks source link

First Class Protocols: Stage 1 #31

Open hzoo opened 6 years ago

hzoo commented 6 years ago

Champion: @michaelficarra Repo: https://github.com/michaelficarra/proposal-first-class-protocols Slides: https://docs.google.com/presentation/d/1WrvSyslnF-5VnPj3k3HRq8MRzuiSN1kQ6ENE1iUSmDU/edit?usp=sharing First presented at the Sept 2017 meeting

Example

protocol ProtocolName {
  // declare a symbol which must be implemented
  thisMustBeImplemented;

  // and some methods that you get for free by implementing this protocol
  youGetThisMethodForFree(...parameters) {
      methodBody;
  }
}

class ClassName implements ProtocolName {
  [ProtocolName.thisMustBeImplemented]() {
    // this is the implementation for this class
  }
}

let instance = new ClassName;
instance[ProtocolName.youGetThisMethodForFree]();

Implementation

littledan commented 6 years ago

I'm excited to see more feedback on this proposal through Babel!