Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ecmascript-proposal-first-class-protocol

 ecmascript-proposal-first-class-protocol

EcmascriptのFirst class protocolの提案について

More Decks by Taketoshi Aono(青野健利 a.k.a brn)

Other Decks in Programming

Transcript

  1. proposal-first-class-protocol

    View Slide

  2. せ⵸: @brn (ꫬꅿ⨳ⵃa.k.a ـٕ٦ظ)
    耵噟: ؿٗٝزؒٝسؒٝآص،٥iOSؒٝآص،
    ⠓爡: Cyberagent ،سذؙأةآؔRightSegment٥AI Messenger
    ـؚٗ: http://abcdef.gets.b6n.ch/
    Twitter: https://twitter.com/brn227
    GitHub: https://github.com/brn

    View Slide

  3. What is this proposal ?
    protocolך䲿周
    ⯋ղכinterfaceך䲿周׌׏׋կ

    View Slide

  4. What is 'protocol' ?
    classָ㹋鄲ׅץֹ鋉秈׾邌ׅկ
    㹋䡾כקה׿וinterfaceהずׄדծ
    swiftװobjective-cדכinterface׾protocolהㄎןծ
    javaדכず瘝ך׮ך׾interfaceהㄎע(稢ְַ鎉铂➬圫♳ך麩ְכ֮
    ׷ָ)

    View Slide

  5. Ecmascript way
    ׃ַ׃Ecmascriptךprotocolכֿך⚕倯ה׮殯ז׷杝荈ך➬圫ָ䲿
    周ׁ׸גְ׷կ

    View Slide

  6. As Symbol
    植㖈䲿周⚥ךֿךproposalכprotocol׾ծ
    ًاحس׾䭯אSymbolך꧊さה׃ג邌ׅկ

    View Slide

  7. protocol ProtocolName {!
    // 実装が必要なシンボルを宣言
    thisMustBeImplemented;!
    !
    // メソッド実装
    youGetThisMethodForFree() {!
    return this[ProtocolName.thisMustBeImplemented]();!
    }!
    }!
    !
    class ClassName implements ProtocolName {!
    [ProtocolName.thisMustBeImplemented]() {!
    ...!
    }!
    } !

    View Slide

  8. Extends Existing class
    傀㶷ךclassח㼎׃ג׮protocol׾㹋鄲ׅ׷ֿהָדֹ׷կ

    View Slide

  9. protocol Functor {!
    map;!
    }!
    !
    Promise.prototype[Functor.map] = () => {!
    ...!
    }!
    !
    Protocol.implement(Promise, Functor);!

    View Slide

  10. 'implements' keyword
    倜׋חimplements怴皾㶨ָ鷄⸇ׁ׸׷

    View Slide

  11. Promise implements Functor // true!
    !
    if (MyClass implements SomeProtocol) { }!

    View Slide

  12. Extends protocol
    protocolכ➭ךprotocol׾䭁䓸דֹ׷

    View Slide

  13. protocol A { a; }!
    protocol B extends A { b; }!
    !
    class C implements B {!
    [A.a]() {}!
    [B.b]() {}!
    }!
    !
    class D implements A {!
    [A.a]() {}!
    }!

    View Slide

  14. Default
    Implementation
    ⴱ劍㹋鄲׮鷄⸇דֹ׷

    View Slide

  15. protocol A { a; }!
    !
    protocol B extends A {!
    [A.a]() { ... }!
    }!

    View Slide

  16. Static protocol
    ꫼涸ًاحس׮protocolדⵖ䖴ׅ׷ֿהָ〳腉

    View Slide

  17. protocol A {!
    static b() {}!
    }!
    !
    class C implements A { }!
    C[A.b]();!

    View Slide

  18. Issues
    傀㶷ךؙٓأ׾䭁䓸ׅ׷㜥さծprototypeפך湫䱸➿Ⰵהְֲ䲿周ָ
    遤׻׸גְ׷կ
    ְֻ׵ז׿ד׮儗➿鹼׸זךדծ➿剏周׾䲿周׃גְֶ׋կ

    View Slide

  19. Proposed 1
    Protocol.impelemtꟼ侧ך痥♲䒷侧ד㹋鄲

    View Slide

  20. protocol Functor {!
    map;!
    }!
    !
    // If implementation is not satisfied,
    throw TypeError.!
    Protocol.implement(Array, Functor, {!
    [Functor.map]: Array.prototype.map!
    });!

    View Slide

  21. Proposed 2
    傀㶷ךؙٓأ׮ⱄ䏝implementsؗ٦ٙ٦سד䭁䓸〳腉ח

    View Slide

  22. protocol Functor {!
    map;!
    }!
    !
    // We can implements protocol after
    declaration.!
    Array implements Functor {!
    [Functor.map]: Array.prototype.map!
    }!

    View Slide

  23. TypeScript?
    ה׶ִ֮׆ծ傀㶷ךinterfaceכ婍ׅד֮׹ֲהךֿהկ
    protocol׾וך״ֲז㘗ה׃ג䪔ֲַכ劢㹀
    Allow dynamic name in typesהְֲ㘗ךفٗػذ؍せחsymbol׾
    ⢪ֲֿהָדֹ׷PRָ鹌׿דְ׷ךדծ
    ֿ׸ָ㹋鄲ׁ׸׷הprotocol׮أي٦ؤחְֽ׷ַ׮կ

    View Slide

  24. ׀幠耮֮׶ָהֲ׀ְׂת׃׋

    View Slide