動機
Smalltalk superpowers紹 super
関わ 何 い
実際OOPSLA 2008 Smalltalk
superpowers Travis Griggs さ
super super 話 模様
特徴
通常
super
ソッ 探索 自分 ー ークラ 開始
super super
書
…
ソッ 探索 自分 ー ークラ ー ー
クラ 開始
ッセー super super super super
書 こ 可能
作
方
Travis
さ
実装
VisualWorks
行わ
い
Squeak
版
作
い 調子 乗 ソッ 探索 自分
サ クラ 開始 sub 作
使
クニック
クニック
(1)
Object >> doesNotUnderstand: aMessage
理解 い ッセー VM
送信さ コー ック
aMessage 送 ッセー 入
Ruby method_missing
オー ーライ ッセー 自分以外
例
: doesNotUnderstand:
実装
MyLogProxy >> doesNotUnderstand: aMessage Logger log: aMessage.
^ realObj perform: aMessage selector
クニック
(2)
CompiledMethod
そ 通 コン イ さ ソッ
各クラ 持 methoDictionary 値 格
納さ い
○ ー ッセー セ クタ ンボ
aClass compiledMethodAt: selector 出
○ aClass >> selector
イ コー い 回
例
: CompiledMethod
ソッ
起動
通常 場合
ord := OrderedCollection with: 1 with: 2. ord add: 3.
ord := OrderedCollection with: 1 with: 2.
method := ord class compiledMethodAt: #add:. method valueWithReceiver: ord arguments: #(3).
クニック
(3)
thisContext
実行中 コン 入 い 特殊変数
○ true, false, nil, self, super 次 第6 予約語
○ 約束事 少 いSmalltalk 希有 存在
典型的 ッ 実装 使わ
thisContext sender 送信者 コン 得
例
: thisContext
アクセ
制御
private ソッ 実装 ※
Object >> private
self class = thisContext sender sender methodClass ifFalse: [SmallTalkAccessViolation private signal]
methodA
self private. ...
使用例
コー
解説
(1)
AnySend いうクラ 定義
ProtoObject subclass: #AnySend
instanceVariableNames: 'receiver searchClass' classVariableNames: ''
poolDictionaries: ''
category: 'SuperSuper'
receiver -> ッセー 手
コー
解説
(2)
AnySend ProtoObject(Object さ 上) 継承
全 ッセー 答え い
doesNotUnderstand: 起動さ
CompiledMethod 出 起動
doesNotUnderstand: aMessage
^((searchClass whichClassIncludesSelector:
aMessage selector) >> aMessage selector) valueWithReceiver: receiver
コー
解説
(3)
Object >> super 用意
AnySend super: supersuper: 作
thisContext 直前 super(変数) 使 い 判断
super
| context sendCode |
context := thisContext sender.
sendCode := context method at: context pc - 2. ^(sendCode == 133)
コー
解説
(4)
AnySend class >> self:
self: anObject
^self receiver: anObject
searchClass: anObject class
生成用 クラ ソッ 定義
super: anObject
^(self self: anObject) super
コー
解説
(5)
super
searchClass := searchClass superclass supersuper: anObject
^(self super: anObject) super う少 生成用 クラ ソッ
AnySend class >> supersuper:
AnySend >> super superclass
調子
乗
sub
追加
(1)
AnySend class >> sub:
sub: anObject
^(self self: anObject) sub
AnySend >> sub
sub
searchClass := searchClass subclass
subclass
^self subclassAt: 1
調子
乗
sub
追加
(2)
Behavior >> subclassAt:
subclassAt: anIndex | subs |
subs := self subclasses
ifEmpty: [self error: 'No subclass']. ^subs at: anIndex
ifAbsent: [self error:
サ
クラ
1
限
い
(1)
methodA
((self sub @ 2) sub @ 3) doSomething
2番目 サ クラ 3番目 サ クラ
doSomething 起動
書 クー !!
methodB
((self sub << ClassA) sub << ClassB) doSomething
サ
クラ
1
限
い
(2)
AnySend class >> sub
@ anInteger
(searchClass inheritsFrom: receiver class)
ifFalse: [self error: '@ can be used after sub']. searchClass := searchClass superclass
subclassAt: anInteger
サ
クラ
1
限
い
(3)
<< aClass
…"@ "
searchClass := searchClass superclass
subclassClassOf: aClass
AnySend >> <<
subclassClassOf: aClass
^self subclassAt: (self subclasses indexOf: aClass)
sub super 使 無限 ー
Top >> doSomethingLoopy self waitASecond.
self doSomething: 'Hello'.
self sub sub doSomethingLoopy.
Middle >> doSomethingLoopy self doSomething: '!!!!'.
super doSomethingLoopy.
Bottom >> doSomethingLoopy self doSomething: 'World'.
Smalltalk
ソッ
探索
仕方
簡単
い
クラ 一 導入 い ソッ
追加
新 言語 作