Polymorphismを知る #TechLunch

Polymorphismを知る #TechLunch

Polymorphismを知る
2013/05/15 (水) @ Livesense TechLunch
発表者:桂 大介

E60aa4f80303f3f386898546ddb3686a?s=128

Livesense Inc.

April 21, 2014
Tweet

Transcript

  1. Polymorphismを知る katsura@livesense.co.jp

  2. Poly-morphism

  3. Luca Cardelli and Peter Wegner. On Understanding Types, Data Abstraction,

    and Polymorphism. Computing Surveys http://phobos.ramapo.edu/~ldant/oop/cardelli_wegner.pdf
  4. parametric vs. inclusion • parametric ◦ compile-time ◦ generic programming

    • inclusion ◦ runtime ◦ subtype polymorphism
  5. Generic Programming Generic programming is a style of computer programming

    in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters.
  6. Overloading Overloading allows functions with the same name act differently

    for each type.
  7. Coercion Coercion happens when an object or a primitive is

    cast into another object type or primitive type.
  8. Explicit cast @ C++ #include <iostream> using namespace std; int

    main() { int yutori_pi = (int) 3.14; cout << yutori_pi << endl; return 1; }
  9. Implicit cast @ C++ #include <iostream> using namespace std; int

    main() { int yutori_pi = 3.14; cout << yutori_pi << endl; return 1; }
  10. Implicit conversion @ Scala scala> val num: Int = "1"

    <console>:7: error: type mismatch; found : java.lang.String("1") required: Int val num: Int = "1" ^ scala> implicit def string2int(s: String) = s.toInt string2int: (s: String)Int scala> val num: Int = "1" num: Int = 1
  11. #define マクr...

  12. Interface vs. DuckTyping • Interface ◦ Java ◦ PHP •

    Structural Subtyping ◦ OCaml ◦ Scala • Template ◦ C++ • Duck Typing ◦ Ruby ◦ Python
  13. <?php interface Cryable { public function cry(); } class Dog

    implements Cryable { public function cry() { return "bowbow"; } } class Cat implements Cryable { public function cry() { return "nya-nya-"; } } class Person { public function punch(Cryable $t) { return $t->cry(); } } $person = new Person; echo $person->punch(new Dog) . PHP_EOL; echo $person->punch(new Cat) . PHP_EOL; PHP Interface
  14. class Dog { def cry() = "bowbow" } class Cat

    { def cry() = "nya-nya-" } class Person { def punch[A <: {def cry(): String}](t: A) = t.cry } val person = new Person println(person.punch(new Dog)) println(person.punch(new Cat)) Scala Structural Subtyping
  15. #include <iostream> #include <string> using namespace std; class Dog {

    public: string cry() { return "bowbow"; } }; class Cat { public: string cry() { return "nya-nya-"; } }; class Person { public: template <class T> string punch(T* t) { return t->cry(); } }; int main() { Person person; cout << person.punch(new Dog) << "\n"; cout << person.punch(new Cat) << "\n"; return 1; } C++ Template
  16. class Dog def cry "bowbow" end end class Cat def

    cry "nya-nya-" end end class Person def punch(t) t.cry end end person = Person.new puts person.punch(Dog.new) puts person.punch(Cat.new) Ruby Duck Typing
  17. まとめ 異なる型を統一的に扱いたい!DRY!no switch! ↓ 抽象化レイヤーを挟む(コードに落とせばinterface) ↓ 捨てられた枝葉末節はどこかに記述しなければいけない ↓ ・言語がうまくやる → Coercion ・データと一緒に定義する

     ・コンパイル時に解決 → ジェネリック  ・ランタイムに解決 → 継承(実装継承に限らず) ・手続きそのものを多重定義する → オーバーロード
  18. ご清聴ありがとうございました