ଟ૬ܕ
w ύϥϝʔλଟ૬Parametric Polymorphism
w ෦ܕଟ૬Subtyping Polymorphism
w ΞυϗοΫଟ૬Ad-hoc Polymorphism
จ຺ʹԠͯ͡ɺҰఆͷ੍ݶͷݩͰɺ
ࣜʹ͍Ζ͍ΖͳܕΛ༩͑Δ
Slide 13
Slide 13 text
෦ܕଟ૬
w ෦ܕ
“String is a Object”
interface X {
void f(Object x);
}
X x = …;
x.f("Ruby");
x.f(new ArrayList());
Slide 14
Slide 14 text
෦ܕଟ૬
w ෦ܕ
“String is a Object”
interface X {
void f(Object x);
}
X x = …;
x.f("Ruby");
x.f(new ArrayList());
ҾͷܕString
Slide 15
Slide 15 text
෦ܕଟ૬
w ෦ܕ
“String is a Object”
interface X {
void f(Object x);
}
X x = …;
x.f("Ruby");
x.f(new ArrayList());
ҾͷܕString
ҾͷܕArrayList
Slide 16
Slide 16 text
໊લతɾߏత
w ໊લతNominal
ΫϥεఆٛͰએݴͯ͠෦ܕؔΛఆٛ
w ߏతStructual
ϝιουఆٛͷแؚؔͰ෦ܕؔΛఆٛʢΑΓ
ॊೈʣ
Slide 17
Slide 17 text
ܕਪ
w ʢશͳʣܕਪ
w ϩʔΧϧܕਪ
Slide 18
Slide 18 text
ʢશͳʣܕਪ
[Milner, 1978]
w શ͘ܕΛॻ͔ͳͯ͘ྑ͍
ʢܕਪͷશੑCompletenessʣ
w ϓϩάϥϛϯάݴޠͷ੍͕େ͖͍ʢMLʣ
# let has_even_leaf tree =
exists_leaf (fun n -> n mod 2 = 0) tree;;
val has_even_leaf : tree -> bool =
Slide 19
Slide 19 text
ϩʔΧϧܕਪ
[Pierce, 1997]
w ϩʔΧϧมͷܕॻ͔ͳͯ͘ྑ͍
ϝιουͱ͔ͷܕॻ͘ɻ
w ͍ΖΜͳϓϩάϥϛϯάݴޠʹಋೖͰ͖Δ
ʢScala/Swift/C#/C++ʣ
func greet(person: String) -> String {
let greeting = "Hello, " + person + "!"
return greeting
}
Slide 20
Slide 20 text
ϩʔΧϧܕਪ
[Pierce, 1997]
w ϩʔΧϧมͷܕॻ͔ͳͯ͘ྑ͍
ϝιουͱ͔ͷܕॻ͘ɻ
w ͍ΖΜͳϓϩάϥϛϯάݴޠʹಋೖͰ͖Δ
ʢScala/Swift/C#/C++ʣ
func greet(person: String) -> String {
let greeting = "Hello, " + person + "!"
return greeting
} greetingͷܕਪ͞ΕΔ
Slide 21
Slide 21 text
ϩʔΧϧܕਪ
[Pierce, 1997]
w ϩʔΧϧมͷܕॻ͔ͳͯ͘ྑ͍
ϝιουͱ͔ͷܕॻ͘ɻ
w ͍ΖΜͳϓϩάϥϛϯάݴޠʹಋೖͰ͖Δ
ʢScala/Swift/C#/C++ʣ
func greet(person: String) -> String {
let greeting = "Hello, " + person + "!"
return greeting
} greetingͷܕਪ͞ΕΔ
ؔͷܕॻ͘
eval
w ແཧ
w evalͷฦΓͷܕanyΈ͍ͨͳܕʹͯ͠͠·͑
͍͍ͷͰʢGradual Typing [Siek and Taha, 2006]ʣ
eval(gets)
Slide 31
Slide 31 text
ActiveRecord
w DBʹଓ͠ͳ͍ͱΞτϦϏϡʔτ͕Θ͔Βͳ͍
w anyͰͳΜͱ͔͠Α͏
class Person < ApplicationRecord
belongs_to :group
has_many :contacts
end
Slide 32
Slide 32 text
ϝλϓϩάϥϛϯά
define_method(:name=) do |x|
@attributes[:name] = x
end
Slide 33
Slide 33 text
ϝλϓϩάϥϛϯά
class Location
attr_reader :line
attr_reader :column
end
Slide 34
Slide 34 text
require
w require͕࣮ߦ͞ΕΔॠؒ·ͰͲͷϓϩάϥϜ͕ಡ·
ΕΔ͔Θ͔Βͳ͍
w requireϝιουͳͷͰɺ࠶ఆٛͰ͖Δ͠ɺͲ͜
ʹͰॻ͚Δ͠ɺҾʹ͕ࣜॻ͚Δ
w $LOAD_PATH͋ΔΑʂ
w requireΛߏจʹͯ͠จࣈྻ͔͠ड͚औΕͳ͍ˍτο
ϓϨϕϧʹ͔͠ॻ͚ͳ͍Α͏ʹ͢Εྑ͍ͷͰ
Slide 35
Slide 35 text
RubyGems
⚰Bundler
Slide 36
Slide 36 text
RubyΛ੍ݶͯ͠͏·͍͔͘ͳ͍
w ଞʹ͕͋Γɺݱ࣮తͳ੍ݶ͕ݟ͚ͭΒΕͳ͍
ʢࠓޙɺݟ͔ͭΔ͔͠Εͳ͍ʣ
w ܕΛॻ͔ͳ͍લఏͰɺҰճanyʹࣦΘΕͨܕใ
ΛऔΓ͢ํ๏͕ͳ͍
Slide 37
Slide 37 text
զʑ͕ཉ͍͠ͷ
ύϥϝʔλଟ૬ߏతͳ෦ܕଟ૬
શͳܕਪRuby
Slide 38
Slide 38 text
͝ఏҊ
w ߏతͳ෦ܕଟ૬ϩʔΧϧܕਪGradual Typing
w ʢΘΓͱʣDuck TypingͰ͖ΔͷͰخ͍͠
w ॻ͔ͳͯ͘ྑ͍ܕॻ͔ͳ͍
w ܕ͕ॻ͍ͯͳ͍ͱ͖ࠓͷRubyͱಉ͡
w ܕͷͨΊͷݴޠRubyͱผ
requireͱ͔ΦʔϓϯΫϥεͱ͔ϝλϓϩάϥϛϯάͱ
͔ߟ͑ͳ͍ɻ
Slide 39
Slide 39 text
͍͍ͩͨTypeScript
Slide 40
Slide 40 text
# group.rbi
interface String
def gsub: (Regexp, String) -> String
end
interface IGroup
def people: ActiveRecord.Association
end
interface IPerson
def email: String
end
# foo_controller.rb
group: IGroup = Group.create!(name: name)
person = group.people.new
escaped = person.email.map {|x| … }
Slide 41
Slide 41 text
# group.rbi
interface String
def gsub: (Regexp, String) -> String
end
interface IGroup
def people: ActiveRecord.Association
end
interface IPerson
def email: String
end
# foo_controller.rb
group: IGroup = Group.create!(name: name)
person = group.people.new
escaped = person.email.map {|x| … }
ผͷϑΝΠϧͰɺΠϯλʔϑΣΠεΛએݴ͢Δ
ܕऍ͕ͳ͚Ε͜Ε·ͰͷRubyͱಉ͡
group = Group.create!(name: name)
person = group.people.new
escaped = person.email.map {|x| … }
Slide 47
Slide 47 text
ܕऍ͕ͳ͚Ε͜Ε·ͰͷRubyͱಉ͡
group = Group.create!(name: name)
person = group.people.new
escaped = person.email.map {|x| … }
ܕऍ͕ͳ͍ͷͰશ෦anyʹͳΔԿݕࠪ͠ͳ͍
Slide 48
Slide 48 text
ܕऍ͕ͳ͚Ε͜Ε·ͰͷRubyͱಉ͡
group = Group.create!(name: name)
person = group.people.new
escaped = person.email.map {|x| … }
map͕ແ͍͜ͱ࣮ߦ࣌ʹൃ֮
ܕऍ͕ͳ͍ͷͰશ෦anyʹͳΔԿݕࠪ͠ͳ͍
Slide 49
Slide 49 text
# group.rbi
interface String
def gsub: (Regexp, String) -> String
...
end
interface IGroup
def people: ActiveRecord.Association
end
interface IPerson
def email: String
end
# foo_controller.rb
group: IGroup = Group.create!(name: name)
person = group.people.new
escaped = person.email.map {|x| … }
Slide 50
Slide 50 text
– RubyͷMatz
lܕઈରॻ͖ͨ͘ͳ͍z
Slide 51
Slide 51 text
w Group.create!ͷܕ͕GroupʹͳΔͷΘ͔Βͳ͍ͷʁ
Groupͷܕ͕Θ͔ΔͱࢥͬͯΔͷʁʁ
w ܕΛҰͭॻ͍͍ͯͳ͍ͱͳʹΘ͔Βͳ͍ͷʁʁ
ϦςϥϧͳΜ͔Θ͔Δͣɻҙ֎ͱ͍Ζ͍Ζݟ͔ͭΔͷͰɻ
w ΫϥεఆٛʹܕΛॻ͚ͳ͍ͷʁΦʔϓϯΫϥεͲ͏ͳΔͷʁ
ࠓޙͷ՝ɻΦʔϓϯΫϥεͱ0CKFDUJWF$ͷ$BUFHPSZͳΜ͔
͍ۙؾ͕͢Δɻ
w ࣮ͳ͍ͷʁ
Α͘ݟΔͱɺʢܕऍΛίϝϯτͱ͔Ͱॻ͘͜ͱʹ͢Εʣ͙͢
ʹ࣮Ͱ͖Δͳ͜Εʜʜ