Python の例を Smalltalk で強引に書いてみる

某所で Python を使って呈示されている例を移植してみました。でも、Smalltalk だとやっていることや意味は同じでも、雰囲気や使い勝手はがらりと変わってしまいますね。

mySwitch
mySwitch _ PySwitch subclass: #MySwitch instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Category-UserDefined'. mySwitch compile: 'a ^ ''aho''' classified: 'casing'; compile: 'b ^ ''boke''' classified: 'casing'; compile: 'c ^ ''chobi''' classified: 'casing'; compile: 'default: sym ^ ''i don''''t know: '', sym' classified: 'casing'. mySwitch caseIs: 'a' "=> 'aho' "
mySwitch caseIs: 'x'   "=> 'i don''t know: x' "

前提となる定義はこちら。

'From SqueakNihongo6.1 of 17 April 2004 [latest update: #0]'!
Object subclass: #PySwitch
   instanceVariableNames: ''
   classVariableNames: ''
   poolDictionaries: ''
   category: 'Kernel-Objects'!

!PySwitch methodsFor: 'message dispatching'!
default: aSymbol
   ^ self subclassResponsibility! !

!PySwitch methodsFor: 'message dispatching'!
doesNotUnderstand: aMessage
   ^ self default: aMessage selector! !

!PySwitch class methodsFor: 'instance creation'!
caseIs: aString
   ^ self new perform: aString asSymbol! !