Slide 1

Slide 1 text

Ծ૝ؔ਺ςʔϒϧͱ ܕΫϥεΛݟൺ΂Δ ҏಸ ྛଠ࿠ id:tarao @tarao @oarat 2024-01-26 ୈޒճؔ਺ܕϓϩάϥϛϯά (Ծ) ͷձ

Slide 2

Slide 2 text

ࣗݾ঺հ ໊લ ɹ ͍ ͳ ҏಸ ɹ ɹ ΓΜͨΖ͏ ྛଠ࿠ ɹ ( id:tarao @tarao @oarat) 2021-02 ∼ ͸ͯͳ ΤϯδχΞϦϯάϚωʔδϟ 2013-04 ∼ ͸ͯͳ ΤϯδχΞ 2010-04 ∼ ژ౎େֶ ৘ใֶݚڀՊ (ࠤ౻ɾޒेཛྷݚ) ത࢜ޙظ՝ఔ / ೔ຊֶज़ৼڵձ ಛผݚڀһ (DC1) ࠷ۙ ▶ record4s: extensible records for Scala ▶ ͋ΒΏΔϓϩάϥϛϯάݴޠͷ࠷ઌ୺Λߦ ͘ Scala 3 ͷϚΫϩ ੲ ▶ L. Ina and A. Igarashi. Gradual Typing for Generics. In Proc. of OOPSLA 2011. ▶ “gradual typing” Λʮ઴ਐతܕ෇͚ʯͱ༁ ͨ͠ऀͷ 1 ਓ

Slide 3

Slide 3 text

ܕΫϥεͱOOP SPJ ᐌ͘ “ ▶ A bit like OOP, except that method suite passed separately? ▶ No!! Type classes implement type-based dispatch, not value-based dispatch ▶ S. P. Jones. Classes, Jim, but not as we know them — Type classes in Haskell. In ECOOP 2009.

Slide 4

Slide 4 text

͋ʔ ͦʔΏʔ͜ͱͶ ׬શʹཧղͨ͠

Slide 5

Slide 5 text

͋ʔ ͦʔΏʔ͜ͱͶ ׬શʹཧղͨ͠ . . . ͬͯࢥͬͨͷ͸ tarao ͚͔ͩ΋͠Εͳ͍͔Β ͜ͷ࿩Λ͢Δ͜ͱʹ͠·ͨ͠ (tarao ʹͱͬͯ͸౰ͨΓલͷ࿩͔͠͠·ͤΜ)

Slide 6

Slide 6 text

OOPͱܕΫϥεͷҙຯ࿦Λൺֱ

Slide 7

Slide 7 text

ݴޠػೳͷҙຯ࿦ ݴޠ L ͷػೳ F ▶ L ⇝ (L − F) ͷม׵Ͱఆٛ͢Δ (࣮ࡍͷॲཧܥʹ͓͚Δ౶ҥߏจͷ desugar ʹ૬౰) ྫ ▶ ݴޠ L : Haskell ▶ ػೳ F : ܕΫϥε ▶ ม׵ : ܕΫϥε͋Γͷ Haskell ⇝ ܕΫϥεͳ͠ͷ Haskell (Wadler ’89 ͷ࿦จͰ΋࣮ࡍͦ͏΍ͬͯҙຯ࿦Λ༩͍͑ͯΔ)

Slide 8

Slide 8 text

OOP

Slide 9

Slide 9 text

OOP (෦෼ܕଟ૬ + ಈతσΟεύον) abstract class Pet: def speak(): String class Cat extends Pet: override def speak(): String = "Meow" class Dog extends Pet: override def speak(): String = "Woof" def listen(pet: Pet): String = pet.speak() listen(new Cat) // "Meow" listen(new Dog) // "Woof"

Slide 10

Slide 10 text

OOP (෦෼ܕଟ૬ + ಈతσΟεύον) abstract class Pet: def speak(): String class Cat extends Pet: override def speak(): String = "Meow" class Dog extends Pet: override def speak(): String = "Woof" def listen(pet: Pet): String = pet.speak() listen(new Cat) // "Meow" listen(new Dog) // "Woof" ▶ ҟͳΔܕΛ౷Ұతʹѻ͍͍ͨ

Slide 11

Slide 11 text

OOP (෦෼ܕଟ૬ + ಈతσΟεύον) abstract class Pet: def speak(): String class Cat extends Pet: override def speak(): String = "Meow" class Dog extends Pet: override def speak(): String = "Woof" def listen(pet: Pet): String = pet.speak() listen(new Cat) // "Meow" listen(new Dog) // "Woof" ▶ ڞ௨ͷ্ҐܕΛࢦఆ (෦෼ܕଟ૬)

Slide 12

Slide 12 text

OOP (෦෼ܕଟ૬ + ಈతσΟεύον) abstract class Pet: def speak(): String class Cat extends Pet: override def speak(): String = "Meow" class Dog extends Pet: override def speak(): String = "Woof" def listen(pet: Pet): String = pet.speak() listen(new Cat) // "Meow" listen(new Dog) // "Woof" ▶ ಉ໊ϝιουΛ෦෼ܕ͝ͱʹ࣮૷

Slide 13

Slide 13 text

OOP (෦෼ܕଟ૬ + ಈతσΟεύον) abstract class Pet: def speak(): String class Cat extends Pet: override def speak(): String = "Meow" class Dog extends Pet: override def speak(): String = "Woof" def listen(pet: Pet): String = pet.speak() listen(new Cat) // "Meow" listen(new Dog) // "Woof" ▶ ݺ͹ΕΔͷ͸෦෼ܕͷϝιου࣮૷ (ಈతσΟεύον)

Slide 14

Slide 14 text

OOPͷҙຯ࿦ (Ծ૝ؔ਺ςʔϒϧ (vtable)) abstract class Pet: def speak(): String class Cat extends Pet: override def speak(): String = "Meow" class Dog extends Pet: override def speak(): String = "Woof" def listen(pet: Pet): String = pet.speak() abstract class Pet( val vtable: PetMethods ) class PetMethods(val speak: Pet => String) class Cat extends Pet( CatIsPet ): def speak(): String = "Meow" val CatIsPet = PetMethods((pet: Pet) => pet.asInstanceOf[Cat].speak() ) class Dog extends Pet( DogIsPet ): def speak(): String = "Woof" val DogIsPet = PetMethods((pet: Pet) => pet.asInstanceOf[Dog].speak() ) def listen(pet: Pet): String = pet.vtable .speak(pet) ˞ ࣮ࡍͷॲཧܥͷ࣮૷ͱ͸ҟͳΔ (͋͘·Ͱ֓೦্ͷઆ໌)

Slide 15

Slide 15 text

OOPͷҙຯ࿦ (Ծ૝ؔ਺ςʔϒϧ (vtable)) abstract class Pet: def speak(): String class Cat extends Pet: override def speak(): String = "Meow" class Dog extends Pet: override def speak(): String = "Woof" def listen(pet: Pet): String = pet.speak() abstract class Pet( val vtable: PetMethods ) class PetMethods(val speak: Pet => String) class Cat extends Pet( CatIsPet ): def speak(): String = "Meow" val CatIsPet = PetMethods((pet: Pet) => pet.asInstanceOf[Cat].speak() ) class Dog extends Pet( DogIsPet ): def speak(): String = "Woof" val DogIsPet = PetMethods((pet: Pet) => pet.asInstanceOf[Dog].speak() ) def listen(pet: Pet): String = pet.vtable .speak(pet) ▶ override ͳ͠Ͱॻ͘

Slide 16

Slide 16 text

OOPͷҙຯ࿦ (Ծ૝ؔ਺ςʔϒϧ (vtable)) abstract class Pet: def speak(): String class Cat extends Pet: override def speak(): String = "Meow" class Dog extends Pet: override def speak(): String = "Woof" def listen(pet: Pet): String = pet.speak() abstract class Pet( val vtable: PetMethods ) class PetMethods(val speak: Pet => String) class Cat extends Pet( CatIsPet ): def speak(): String = "Meow" val CatIsPet = PetMethods((pet: Pet) => pet.asInstanceOf[Cat].speak() ) class Dog extends Pet( DogIsPet ): def speak(): String = "Woof" val DogIsPet = PetMethods((pet: Pet) => pet.asInstanceOf[Dog].speak() ) def listen(pet: Pet): String = pet.vtable .speak(pet) ▶ ڞ௨ͷϝιουͷശ (ࣙॻ) Λ༻ҙ͓ͯ͘͠

Slide 17

Slide 17 text

OOPͷҙຯ࿦ (Ծ૝ؔ਺ςʔϒϧ (vtable)) abstract class Pet: def speak(): String class Cat extends Pet: override def speak(): String = "Meow" class Dog extends Pet: override def speak(): String = "Woof" def listen(pet: Pet): String = pet.speak() abstract class Pet( val vtable: PetMethods ) class PetMethods(val speak: Pet => String) class Cat extends Pet( CatIsPet ): def speak(): String = "Meow" val CatIsPet = PetMethods((pet: Pet) => pet.asInstanceOf[Cat].speak() ) class Dog extends Pet( DogIsPet ): def speak(): String = "Woof" val DogIsPet = PetMethods((pet: Pet) => pet.asInstanceOf[Dog].speak() ) def listen(pet: Pet): String = pet.vtable .speak(pet) ▶ ෦෼ܕ͝ͱʹผʑͷϝιουΛ٧Ί͓ͯ͘

Slide 18

Slide 18 text

OOPͷҙຯ࿦ (Ծ૝ؔ਺ςʔϒϧ (vtable)) abstract class Pet: def speak(): String class Cat extends Pet: override def speak(): String = "Meow" class Dog extends Pet: override def speak(): String = "Woof" def listen(pet: Pet): String = pet.speak() abstract class Pet( val vtable: PetMethods ) class PetMethods(val speak: Pet => String) class Cat extends Pet( CatIsPet ): def speak(): String = "Meow" val CatIsPet = PetMethods((pet: Pet) => pet.asInstanceOf[Cat].speak() ) class Dog extends Pet( DogIsPet ): def speak(): String = "Woof" val DogIsPet = PetMethods((pet: Pet) => pet.asInstanceOf[Dog].speak() ) def listen(pet: Pet): String = pet.vtable .speak(pet) ▶ vtable Λܦ༝ͯ͠ϝιουΛݺͿ

Slide 19

Slide 19 text

ܕΫϥε

Slide 20

Slide 20 text

ܕΫϥε (ύϥϝʔλଟ૬ + ΞυϗοΫଟ૬) trait Pet[P] : extension (p: P) def speak(): String class Cat: def speak(): String = "Meow" given Pet[Cat] with extension (cat: Cat) def speak(): String = cat.speak() class Dog: def speak(): String = "Woof" given Pet[Dog] with extension (dog: Dog) def speak(): String = dog.speak() def listen[P: Pet ](pet: P ): String = pet.speak() listen( new Cat ) // "Meow" listen( new Dog ) // "Woof"

Slide 21

Slide 21 text

ܕΫϥε (ύϥϝʔλଟ૬ + ΞυϗοΫଟ૬) trait Pet[P] : extension (p: P) def speak(): String class Cat: def speak(): String = "Meow" given Pet[Cat] with extension (cat: Cat) def speak(): String = cat.speak() class Dog: def speak(): String = "Woof" given Pet[Dog] with extension (dog: Dog) def speak(): String = dog.speak() def listen[P: Pet ](pet: P ): String = pet.speak() listen( new Cat ) // "Meow" listen( new Dog ) // "Woof" ▶ ҟͳΔܕΛ౷Ұతʹѻ͍͍ͨ

Slide 22

Slide 22 text

ܕΫϥε (ύϥϝʔλଟ૬ + ΞυϗοΫଟ૬) trait Pet[P] : extension (p: P) def speak(): String class Cat: def speak(): String = "Meow" given Pet[Cat] with extension (cat: Cat) def speak(): String = cat.speak() class Dog: def speak(): String = "Woof" given Pet[Dog] with extension (dog: Dog) def speak(): String = dog.speak() def listen[P: Pet ](pet: P ): String = pet.speak() listen( new Cat ) // "Meow" listen( new Dog ) // "Woof" ▶ ಛఆͷΠϯλϑΣʔεΛཁٻ (͜Ε͕ܕΫϥε)

Slide 23

Slide 23 text

ܕΫϥε (ύϥϝʔλଟ૬ + ΞυϗοΫଟ૬) trait Pet[P] : extension (p: P) def speak(): String class Cat: def speak(): String = "Meow" given Pet[Cat] with extension (cat: Cat) def speak(): String = cat.speak() class Dog: def speak(): String = "Woof" given Pet[Dog] with extension (dog: Dog) def speak(): String = dog.speak() def listen[P: Pet ](pet: P ): String = pet.speak() listen( new Cat ) // "Meow" listen( new Dog ) // "Woof" ▶ ΠϯλϑΣʔεΛຬͨ͢ূڌ (ܕΫϥεͷΠϯελϯε)

Slide 24

Slide 24 text

ܕΫϥε (ύϥϝʔλଟ૬ + ΞυϗοΫଟ૬) trait Pet[P] : extension (p: P) def speak(): String class Cat: def speak(): String = "Meow" given Pet[Cat] with extension (cat: Cat) def speak(): String = cat.speak() class Dog: def speak(): String = "Woof" given Pet[Dog] with extension (dog: Dog) def speak(): String = dog.speak() def listen[P: Pet ](pet: P ): String = pet.speak() listen( new Cat ) // "Meow" listen( new Dog ) // "Woof" ▶ ূڌʹج͍ͮͯݺͼ෼͚

Slide 25

Slide 25 text

ܕΫϥεͷҙຯ࿦ trait Pet[P]: extension (p: P) def speak(): String class Cat: def speak(): String = "Meow" given Pet[Cat] with extension (cat: Cat) def speak(): String = cat.speak() class Dog: def speak(): String = "Woof" given Pet[Dog] with extension (dog: Dog) def speak(): String = dog.speak() def listen[P: Pet](pet: P): String = pet.speak() class Pet[P](val speak: P => String) class Cat: def speak(): String = "Meow" val CatIsPet = Pet[Cat]((cat: Cat) => cat.speak()) class Dog: def speak(): String = "Woof" val DogIsPet = Pet[Dog]((dog: Dog) => dog.speak()) def listen[P](pet: P) (dict: Pet[P]) : String = dict .speak(pet)

Slide 26

Slide 26 text

ܕΫϥεͷҙຯ࿦ trait Pet[P]: extension (p: P) def speak(): String class Cat: def speak(): String = "Meow" given Pet[Cat] with extension (cat: Cat) def speak(): String = cat.speak() class Dog: def speak(): String = "Woof" given Pet[Dog] with extension (dog: Dog) def speak(): String = dog.speak() def listen[P: Pet](pet: P): String = pet.speak() class Pet[P](val speak: P => String) class Cat: def speak(): String = "Meow" val CatIsPet = Pet[Cat]((cat: Cat) => cat.speak()) class Dog: def speak(): String = "Woof" val DogIsPet = Pet[Dog]((dog: Dog) => dog.speak()) def listen[P](pet: P) (dict: Pet[P]) : String = dict .speak(pet) ▶ ܕΫϥεͷ࣮ମ͸ϝιουͷࣙॻ

Slide 27

Slide 27 text

ܕΫϥεͷҙຯ࿦ trait Pet[P]: extension (p: P) def speak(): String class Cat: def speak(): String = "Meow" given Pet[Cat] with extension (cat: Cat) def speak(): String = cat.speak() class Dog: def speak(): String = "Woof" given Pet[Dog] with extension (dog: Dog) def speak(): String = dog.speak() def listen[P: Pet](pet: P): String = pet.speak() class Pet[P](val speak: P => String) class Cat: def speak(): String = "Meow" val CatIsPet = Pet[Cat]((cat: Cat) => cat.speak()) class Dog: def speak(): String = "Woof" val DogIsPet = Pet[Dog]((dog: Dog) => dog.speak()) def listen[P](pet: P) (dict: Pet[P]) : String = dict .speak(pet) ▶ ΠϯλϑΣʔεͷূڌ͸ࣙॻΠϯελϯε

Slide 28

Slide 28 text

ܕΫϥεͷҙຯ࿦ trait Pet[P]: extension (p: P) def speak(): String class Cat: def speak(): String = "Meow" given Pet[Cat] with extension (cat: Cat) def speak(): String = cat.speak() class Dog: def speak(): String = "Woof" given Pet[Dog] with extension (dog: Dog) def speak(): String = dog.speak() def listen[P: Pet](pet: P): String = pet.speak() class Pet[P](val speak: P => String) class Cat: def speak(): String = "Meow" val CatIsPet = Pet[Cat]((cat: Cat) => cat.speak()) class Dog: def speak(): String = "Woof" val DogIsPet = Pet[Dog]((dog: Dog) => dog.speak()) def listen[P](pet: P) (dict: Pet[P]) : String = dict .speak(pet) ▶ ܕΫϥεΛ࢖͏ϝιου͸ࣙॻΛऔΔܗʹม׵

Slide 29

Slide 29 text

ܕΫϥεͷҙຯ࿦ trait Pet[P]: extension (p: P) def speak(): String class Cat: def speak(): String = "Meow" given Pet[Cat] with extension (cat: Cat) def speak(): String = cat.speak() class Dog: def speak(): String = "Woof" given Pet[Dog] with extension (dog: Dog) def speak(): String = dog.speak() def listen[P: Pet](pet: P): String = pet.speak() listen(new Cat) listen(new Dog) class Pet[P](val speak: P => String) class Cat: def speak(): String = "Meow" val CatIsPet = Pet[Cat]((cat: Cat) => cat.speak()) class Dog: def speak(): String = "Woof" val DogIsPet = Pet[Dog]((dog: Dog) => dog.speak()) def listen[P](pet: P) (dict: Pet[P]) : String = dict .speak(pet) listen(new Cat) (CatIsPet) listen(new Dog) (DogIsPet)

Slide 30

Slide 30 text

ൺֱ

Slide 31

Slide 31 text

ද૚Λݟൺ΂Δ OOP abstract class Pet : def speak(): String class Cat extends Pet : override def speak(): String = "Meow" class Dog extends Pet : override def speak(): String = "Woof" def listen(pet: Pet ): String = pet.speak() ܕΫϥε trait Pet[ P ]: extension (p: P ) def speak(): String class Cat: def speak(): String = "Meow" given Pet[ Cat ] with extension (cat: Cat) def speak(): String = cat.speak() class Dog: def speak(): String = "Woof" given Pet[ Dog ] with extension (dog: Dog) def speak(): String = dog.speak() def listen[P: Pet](pet: P ): String = pet.speak()

Slide 32

Slide 32 text

ද૚Λݟൺ΂Δ OOP abstract class Pet : def speak(): String class Cat extends Pet : override def speak(): String = "Meow" class Dog extends Pet : override def speak(): String = "Woof" def listen(pet: Pet ): String = pet.speak() ܕΫϥε trait Pet[ P ]: extension (p: P ) def speak(): String class Cat: def speak(): String = "Meow" given Pet[ Cat ] with extension (cat: Cat) def speak(): String = cat.speak() class Dog: def speak(): String = "Woof" given Pet[ Dog ] with extension (dog: Dog) def speak(): String = dog.speak() def listen[P: Pet](pet: P ): String = pet.speak() ▶ ෦෼ܕଟ૬ vs. ύϥϝʔλଟ૬

Slide 33

Slide 33 text

ද૚Λݟൺ΂Δ OOP abstract class Pet : def speak(): String class Cat extends Pet : override def speak(): String = "Meow" class Dog extends Pet : override def speak(): String = "Woof" def listen(pet: Pet ): String = pet.speak() ܕΫϥε trait Pet[ P ]: extension (p: P ) def speak(): String class Cat: def speak(): String = "Meow" given Pet[ Cat ] with extension (cat: Cat) def speak(): String = cat.speak() class Dog: def speak(): String = "Woof" given Pet[ Dog ] with extension (dog: Dog) def speak(): String = dog.speak() def listen[P: Pet](pet: P ): String = pet.speak() ▶ ؔ܎͕ܕͷఆٛͱີ݁߹ vs. ؔ܎͕ޙ෇͚

Slide 34

Slide 34 text

ද૚Λݟൺ΂Δ OOP abstract class Pet : def speak(): String class Cat extends Pet : override def speak(): String = "Meow" class Dog extends Pet : override def speak(): String = "Woof" def listen(pet: Pet ): String = pet.speak() ܕΫϥε trait Pet[ P ]: extension (p: P ) def speak(): String class Cat: def speak(): String = "Meow" given Pet[ Cat ] with extension (cat: Cat) def speak(): String = cat.speak() class Dog: def speak(): String = "Woof" given Pet[ Dog ] with extension (dog: Dog) def speak(): String = dog.speak() def listen[P: Pet](pet: P ): String = pet.speak() ▶ ಉ໊ͰΦʔόʔϥΠυ vs. όϥόϥʹ࣮૷ (ผ໊Ͱ΋Մ)

Slide 35

Slide 35 text

ҙຯ࿦Λݟൺ΂Δ OOP abstract class Pet ( val vtable: PetMethods ) class PetMethods (val speak: Pet => String) class Cat extends Pet(CatIsPet) : def speak(): String = "Meow" val CatIsPet = PetMethods ((pet: Pet ) => pet .asInstanceOf[Cat] .speak()) class Dog extends Pet(DogIsPet) : def speak(): String = "Woof" val DogIsPet = PetMethods ((pet: Pet ) => pet .asInstanceOf[Dog] .speak()) def listen(pet: Pet ): String = pet.vtable .speak(pet) ܕΫϥε class Pet[P] (val speak: P => String) class Cat: def speak(): String = "Meow" val CatIsPet = Pet[Cat] ((cat: Cat ) => cat.speak()) class Dog: def speak(): String = "Woof" val DogIsPet = Pet[Dog] ((dog: Dog ) => dog.speak()) def listen [P] (pet: P ) (dict: Pet[P]) : String = dict .speak(pet)

Slide 36

Slide 36 text

ҙຯ࿦Λݟൺ΂Δ OOP abstract class Pet ( val vtable: PetMethods ) class PetMethods (val speak: Pet => String) class Cat extends Pet(CatIsPet) : def speak(): String = "Meow" val CatIsPet = PetMethods ((pet: Pet ) => pet .asInstanceOf[Cat] .speak()) class Dog extends Pet(DogIsPet) : def speak(): String = "Woof" val DogIsPet = PetMethods ((pet: Pet ) => pet .asInstanceOf[Dog] .speak()) def listen(pet: Pet ): String = pet.vtable .speak(pet) ܕΫϥε class Pet[P] (val speak: P => String) class Cat: def speak(): String = "Meow" val CatIsPet = Pet[Cat] ((cat: Cat ) => cat.speak()) class Dog: def speak(): String = "Woof" val DogIsPet = Pet[Dog] ((dog: Dog ) => dog.speak()) def listen [P] (pet: P ) (dict: Pet[P]) : String = dict .speak(pet) ▶ ෦෼ܕଟ૬ vs. ύϥϝʔλଟ૬

Slide 37

Slide 37 text

ҙຯ࿦Λݟൺ΂Δ OOP abstract class Pet ( val vtable: PetMethods ) class PetMethods (val speak: Pet => String) class Cat extends Pet(CatIsPet) : def speak(): String = "Meow" val CatIsPet = PetMethods ((pet: Pet ) => pet .asInstanceOf[Cat] .speak()) class Dog extends Pet(DogIsPet) : def speak(): String = "Woof" val DogIsPet = PetMethods ((pet: Pet ) => pet .asInstanceOf[Dog] .speak()) def listen(pet: Pet ): String = pet.vtable .speak(pet) ܕΫϥε class Pet[P] (val speak: P => String) class Cat: def speak(): String = "Meow" val CatIsPet = Pet[Cat] ((cat: Cat ) => cat.speak()) class Dog: def speak(): String = "Woof" val DogIsPet = Pet[Dog] ((dog: Dog ) => dog.speak()) def listen [P] (pet: P ) (dict: Pet[P]) : String = dict .speak(pet) ▶ ໊લ͕ҧ͏͚ͩ

Slide 38

Slide 38 text

ҙຯ࿦Λݟൺ΂Δ OOP abstract class Pet ( val vtable: PetMethods ) class PetMethods (val speak: Pet => String) class Cat extends Pet(CatIsPet) : def speak(): String = "Meow" val CatIsPet = PetMethods ((pet: Pet ) => pet .asInstanceOf[Cat] .speak()) class Dog extends Pet(DogIsPet) : def speak(): String = "Woof" val DogIsPet = PetMethods ((pet: Pet ) => pet .asInstanceOf[Dog] .speak()) def listen(pet: Pet ): String = pet.vtable .speak(pet) ܕΫϥε class Pet[P] (val speak: P => String) class Cat: def speak(): String = "Meow" val CatIsPet = Pet[Cat] ((cat: Cat ) => cat.speak()) class Dog: def speak(): String = "Woof" val DogIsPet = Pet[Dog] ((dog: Dog ) => dog.speak()) def listen [P] (pet: P ) (dict: Pet[P]) : String = dict .speak(pet) ▶ ஋͔ΒऔΓग़͢ vs. Ҿ਺Ͱ౉͢

Slide 39

Slide 39 text

ҙຯ࿦Λݟൺ΂Δ OOP abstract class Pet ( val vtable: PetMethods ) class PetMethods (val speak: Pet => String) class Cat extends Pet(CatIsPet) : def speak(): String = "Meow" val CatIsPet = PetMethods ((pet: Pet ) => pet .asInstanceOf[Cat] .speak()) class Dog extends Pet(DogIsPet) : def speak(): String = "Woof" val DogIsPet = PetMethods ((pet: Pet ) => pet .asInstanceOf[Dog] .speak()) def listen(pet: Pet ): String = pet.vtable .speak(pet) ܕΫϥε class Pet[P] (val speak: P => String) class Cat: def speak(): String = "Meow" val CatIsPet = Pet[Cat] ((cat: Cat ) => cat.speak()) class Dog: def speak(): String = "Woof" val DogIsPet = Pet[Dog] ((dog: Dog ) => dog.speak()) def listen [P] (pet: P ) (dict: Pet[P]) : String = dict .speak(pet) ▶ ͋ͱ͸ಉ͡!!

Slide 40

Slide 40 text

·ͱΊ

Slide 41

Slide 41 text

ࣅ͍ͯΔͱ͜Ζ ▶ ҟͳΔܕΛ౷Ұతʹѻ͏͘͠Έ ▶ ϝιουͷࣙॻΛ࢖ͬͯݺͼ෼͚Δ

Slide 42

Slide 42 text

ҧ͏ͱ͜Ζ OOP ܕΫϥε ଟ૬ ෦෼ܕଟ૬ ύϥϝʔλଟ૬ ܕͷؔ܎ ີ݁߹ ޙ෇͚Մ ˞ ϝιου࣮૷ ಉ໊ͰΦʔόʔϥΠυ ໌ࣔతʹࢦఆ ࣙॻͷ౉͠ํ ஋ʹೖΕΔ Ҿ਺Ͱ౉͢ ࣙॻͷղܾ ಈత ੩త ˞ ͜ͷͨΊʹܕΫϥεͷΠϯελϯεղܾͷ࢓૊Έ͕ඞཁ ▶ ࿦ཧܕϓϩάϥϛϯάΈ͍ͨʹͳΔ

Slide 43

Slide 43 text

͓ΘΓ