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

Opaque Argument Type with type system

Opaque Argument Type with type system

C114c127f13522da92cb03e5d14946ca?s=128

Takuma Shimizu

April 12, 2019
Tweet

Transcript

  1. Opaque Argument Type Λͻ΋ͱ͘ .PCJMF"DU04","

  2. w ਗ਼ਫ୓ຏ w!IPLVSPO wυϝΠϯۦಈઃܭ w J04 w 3VCZPO3BJMT w 3VTU

    wϑΣϯϦϧגࣜձࣾ
  3. IUUQTRJJUBDPNLPIFSJUFNTEGEDFG

  4. ͷ

  5. Opaque Argument Type Λͻ΋ͱ͘

  6. Opaque Argument Type w Ҿ਺ͷ৔ॴʹҐஔ͢Δsome Protocol w ௨ৗͷHFOFSJDTͱಉ͡ৼΔ෣͍ w TQFDJBMJ[BUJPO

    w ݺͼग़͠ଆ͕۩ମతͳܕΛܾఆ
  7. Opaque Argument Type // generics func useAnimal<A: Animal>(_ animal: A)

    { animal.foo() } // Opaque Argument Type func useAnimal(_ animal: some Animal) { animal.foo() }
  8. Opaque Argument Type // generics func useAnimal<A: Animal>(_ animal: A)

    { animal.foo() } // Opaque Argument Type func useAnimal(_ animal: some Animal) { animal.foo() } 㲈 ಉ౳
  9. Opaque Argument Type // generics func useAnimal<A: Animal>(_ animal: A)

    { animal.foo() } // Opaque Argument Type func useAnimal(_ animal: some Animal) { animal.foo() } 㲈 ಉ౳ ˡͳΜͰʁ
  10. ྔԽ 2VBOUJpDBUJPO

  11. 㱼 w શশྔԽࢠීวʙ w VOJWFSTBMRVBOUJpFS w HFOFSJDT શশه߸ 㱼ΨϯμϜͰ͓ͳ͡ Έͷه߸

  12. 㱽 w ଘࡏྔԽࢠ w FYJTUFOUJBMRVBOUJpFS w 0QBRVF"SHVNFOU5ZQF ଘࡏه߸ ΧλΧφͷ ʮϤʯͬΆ͍

  13. func useAnimal<A: Animal>(_ animal: A) { animal.foo() } ∀ A.

    (func(A: Animal) -> ()) 䁭 શশྔԽܕ func(∃ A. A: Animal) -> () 䁭 ଘࡏྔԽܕ func useAnimal(_ animal: some Animal) { animal.foo() }
  14. (∀ x. (P(x) → Q)) 䁭 શশྔԽܕ ((∃ x. P(x))

    → Q) 䁭 ଘࡏྔԽܕ ∀ A. (func(A: Animal) -> ()) func(∃ A. A: Animal) -> ()
  15. (∀ x. (P(x) → Q)) ((∃ x. P(x)) → Q)

    㱻 㱻 ͜ͷه߸͸ʮಉ஋ʯͷҙຯͰ͢
  16. • func(∃ S. S: Foo) -> T ͕͋Δͱ͖ • ∀

    S. (func(S: Foo) -> T) ΛߏஙͰ͖Δ • ٯ΋·ͨવΓ    
 • func(∃ S. S: Foo) -> T ͕͋Δͱ͖ • ∀ S. (func(S: Foo) -> T) ΛߏஙͰ͖Δ • ٯ΋·ͨવΓ w ௚؍ओٛ࿦ཧʹ͓͍ͯ੒Γཱͭ w ΧϦʔʹϋϫʔυಉܕରԠ w ໋୊ʹܕ GPSNVMBFBTUZQFT  w ͜ΕʹΑ໋ͬͯ୊ͱܕͷؒʹ͸௚઀తͳରԠؔ܎͕͋Δ͜ͱ͕ ɹ໋୊ (∀ x. (P(x) → Q)) 㱻((∃ x. P(x)) → Q)
  17. (∀ x. (P(x) → Q)) ((∃ x. P(x)) → Q)

  18. (∀ x. (P(x) → Q)) ((∃ x. P(x)) → Q)

  19. func useAnimal<A: Animal>(_ animal: A) func useAnimal(_ animal: some Animal)

  20. ಉҰͰ͸ͳ͘ಉܗ ಉܗ ɹɹࣅͨߏ଄Λ࣋ͭ΋ͷ

  21. func useAnimal<A: Animal>(_ animal: A) func useAnimal(_ animal: some Animal)

    શশʹର͢ΔྔԽ ͕ʮଘࡏʯ
  22. Opaque Argument Type w HFOFSJDT શশྔԽ ͱΑ͘ࣅͨߏ଄Λ࣋ͭ w Ϣχόʔαϧ ීวɾશশ

    ʹ͸ͳΒͳ͍ w ಛఆͷ࣮ࡏ ଘࡏ ͢ΔܕΛඞͣ࣋ͭ ଘࡏྔԽ ࿩͠଍Γͳ͍ʜ
  23. ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠