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

Scalaの線形化と抽象型メンバーの統一のジレンマ

s10myk4
March 17, 2017

 Scalaの線形化と抽象型メンバーの統一のジレンマ

セプテーニ・オリジナル 下村 朋之(@s10myk47)
@市ヶ谷Geek★Night#12 ScalaMatsuriの余韻トーク〜祭りの余熱〜


s10myk4

March 17, 2017
Tweet

More Decks by s10myk4

Other Decks in Programming

Transcript

  1. ScalaͷઢܗԽͱந৅ܕϝϯόʔ ͷ౷ҰͷδϨϯϚ ࢢϲ୩Geek˒Night#12 ScalaMatsuriͷ༨ӆτʔΫʙࡇΓͷ༨೤ʙ
 ηϓςʔχɾΦϦδφϧ Լଜ ๎೭(@s10myk47)

  2. ABOUT ME name Լଜ ๎೭(Shimomura Tomoyuki) twitter @s10myk47 organization גࣜձࣾηϓςʔχɾΦϦδφϧ

    team ਓࣄʹ͓͚Δ࠾༻΍഑ଐΛ࠷దԽ͢ΔαʔϏε DDD, Scala, ࠾༻, μʔπ, αοΧʔ
  3. ந৅ܕϝϯόʔ trait A { ɹtype B } ఆٛΛͤͣʹtypeΩʔϫʔυʹΑͬͯந৅ܕΛϝϯόʔͱͯ͠ એݴ͢Δ͜ͱ͕Ͱ͖Δ

  4. ্ݶڥք type B <: T Any …. T ….

  5. ࣗ෼ܕΞϊςʔγϣϯ trait A {
 self:B => } τϨΠτ͕۩৅Ϋϥεʹؔ͢ΔཁٻΛࢦఆͯ͠ɺBΛલఏͱ͢ ΔΑ͏ʹࢦఆͰ͖Δ

  6. SAMPLE CODE trait A {
 type B <: Any
 val

    b:B
 }
 
 trait ListA {
 self:A =>
 type B <: List[_]
 b.size
 }
  7. MOTIVATION

  8. SAMPLE CODE trait A {
 type B <: Any
 val

    b:B
 }
 
 trait ListA {
 self:A =>
 type B <: List[_]
 b.size
 } ListAͷ ந৅ܕϝϯόʔB͸ɺListܕΛ্ݶڥքͱ͢ΔͷͰ bͷ αΠζΛऔಘͰ͖Δͱ͜Λظ଴͢Δ
  9. Oops! scala> trait ListA { | self:A => | type

    B <: List[_] | b.size | } <console>:16: error: value size is not a member of ListA.this.B b.size ^
  10. Why..? scalac͸ઢܗԽͷϓϩηεʹ͓͍ͯɺ ෳ਺ͷtraitʹఆٛ͞Ε͍ͯΔந৅ܕϝϯόʔΛ·ͱΊ ͯɺશͯͷ੍໿Λຬͨ͢ந৅ܕϝϯόʔʹ౷Ұ͢Δ͜ ͱ͕Ͱ͖ͳ͍

  11. Why..? ୅ΘΓʹ… ந৅ܕϝϯόʔ͸ઢܗԽͷϓϩηεʹ͓͍ͯ࠷ޙͷ΋ ͷ͕࠾༻͞ΕɺͦΕ͕શͯͷ੍໿Λຬ͔ͨ͢Ͳ͏͔Λ ޙͰνΣοΫ͢Δͱ͍͏ํ๏

  12. SAMPLE CODE trait A {
 type B <: Any
 val

    b:B
 }
 
 trait ListA {
 self:A =>
 type B <: List[_]
 b.size
 }
  13. Why..? ઢܗԽͷࡍʹந৅ܕϝϯόʔBΛ·ͱΊͯ 
 ͱͯ͠΄͍͕͠ɺɺɺ type B <: List[_] <: Any

  14. Why..? ࠷ޙʹདྷͨ B ͕࠾༻͞ΕΔ
 ListAͷޙʹA͕དྷΔͷͰɺ ந৅ܕϝϯόʔ B ͸ AͷB ͕࠾༻͞ΕΔ

    trait ListA {
 self:Aɹ=>
 type B <: List[_]
 b.size
 }
  15. Why..? trait A {
 type B <: Any
 val b:B


    }
 
 trait ListA {
 self:A =>
 type B <: List[_]
 b.size
 } trait AͷB͸ AnyΛܧঝ͢Δͱ͍͏੍໿͔࣋ͬͯ͠ͳ͍ Anyʹsizeؔ਺ͳ͍ͷͰ…
  16. Oops! scala> trait ListA { | self:A => | type

    B <: List[_] | b.size | } <console>:16: error: value size is not a member of ListA.this.B b.size ^
  17. What should I do ? trait ListA {
 self:A with

    ListA =>
 type B <: List[_]
 b.size
 } ࣗ෼ܕΞϊςʔγϣϯͰઢܗԽͷॱ൪Λ ListA ͕ޙʹདྷΔΑ ͏ʹ໌ࣔ͢Δ
  18. DOT FIXES THIS! Martin Odersky added a comment - 15/Mar/13

    8:30 AM 
 Paul, you are correct on all counts. This is indeed a fundamental problem in Scala's type system (also in its theory, \nu-Obj): Abstract type members are linearized and the last one wins. It is later checked that that last one has a constraint that implies all others. But there is never a unification of constraints. DOT fixes this.
  19. TRY ؀ڥߏஙʹ͍ͭͯ͸ͪ͜Β http://dotty.epfl.ch/#getting-started

  20. SUMMARY ɾscalac͸ઢܗԽͷϓϩηεʹ͓͍ͯɺ ෳ਺ͷtraitʹఆٛ͞Ε͍ͯΔந৅ܕϝϯόʔΛ·ͱΊ ͯɺશͯͷ੍໿Λຬͨ͢ந৅ܕϝϯόʔʹ౷Ұ͢Δ͜ ͱ͕Ͱ͖ͳ͍ ɾࣗ෼ܕΞϊςʔγϣϯͰઢܗԽͷॱ൪Λ ListA ͕ޙʹདྷΔ Α͏ʹ໌ࣔͯ͠ճආ ɾDottyͰ͸fixͨ͠Α

  21. ηϓςʔχΦϦδφϧੵۃ࠾༻தʂ DDD Scala TDD Angular2 Swift3 Android εΫϥϜ ࡋྔ࿑ಇ