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

Polymorphismを知る #TechLunch

Polymorphismを知る #TechLunch

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

Livesense Inc.

April 21, 2014
Tweet

More Decks by Livesense Inc.

Other Decks in Technology

Transcript

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

    and Polymorphism. Computing Surveys http://phobos.ramapo.edu/~ldant/oop/cardelli_wegner.pdf
  2. 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.
  3. Coercion Coercion happens when an object or a primitive is

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

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

    main() { int yutori_pi = 3.14; cout << yutori_pi << endl; return 1; }
  6. 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
  7. Interface vs. DuckTyping • Interface ◦ Java ◦ PHP •

    Structural Subtyping ◦ OCaml ◦ Scala • Template ◦ C++ • Duck Typing ◦ Ruby ◦ Python
  8. <?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
  9. 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
  10. #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
  11. 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
  12. まとめ 異なる型を統一的に扱いたい!DRY!no switch! ↓ 抽象化レイヤーを挟む(コードに落とせばinterface) ↓ 捨てられた枝葉末節はどこかに記述しなければいけない ↓ ・言語がうまくやる → Coercion ・データと一緒に定義する

     ・コンパイル時に解決 → ジェネリック  ・ランタイムに解決 → 継承(実装継承に限らず) ・手続きそのものを多重定義する → オーバーロード