protos をちょっと使ってみる
昔みたいに Mixin のために何か細工する必要はなくなった。
http://d.hatena.ne.jp/shinichiro_h/20031208#p2
以下テストしてみたコード。
AP := Object clone do(
ap := method(write("ap\n"))
apa := method(ap())
apb := method(ap())
)
A := AP clone do(
a := method(write("a\n"))
abc := method(a())
ab := method(a())
apa := method(a())
)
B := Object clone do(
b := method(write("b\n"))
abc := method(b())
ab := method(b())
bc := method(b())
apb := method(b())
)
C := Object clone do(
c := method(write("c\n"))
abc := method(c())
bc := method(c())
)
D := A clone
D appendProto(B)
D appendProto(C)
d := D clone
d a
d b
d c
d abc
d ab
d bc
d ap
d apa
d apb
D は A, B, C を継承していて、 A は AP を継承している。 D は A を継承するさい、 A を clone して(つまり最初にセットした proto は A) 、 appendProtoで B, C をこの順で追加している。つまり以下のような絵。
AP | A B C | | | +-+-+ | D
結果は以下の通り。
a b c a a b ap a ap
メソッド探索の順序は、 A > AP > B > C ということだろう。まぁ普通。
writeln(D protos type)
などとすると List 。普通に foreach などが呼出せるから、かなりラクにリフレクションができそう。