Does Smalltalk(especially Squeak/Pharo) have some form of variadic functions?
I was just reading about the power of designing your own control statments in smalltalk and while I'm a big fan of ifTrue: ifFalse: I was having a hard time coming up with a good way to implement arbitrary if,if else, if else,...,else statements thinking how useful Variadic functions those would be for implementing case statements. Something like
false class
ifTrue: aBlock (... elseIf: aBoolean then: aSecondBlock ...) else: aLastBlock
vArgList pairsDo: [:x :y| x ifTrue:[^ (y value)] ].
^ aLastBlock
Smalltalk's keyword-like syntax for method calls inherently defines the arity of the method. There's no &rest
pattern like in Common Lisp.
You can of course have a method take a list, like BlockClosure>>valueWithArguments:
but that's hardly the same thing.
You might modify Compiler
to support variadic method calls. Maybe the call would simply have with:
between each of the variables:
(condition) ifTrue: aBlock elseIf: aBoolean with: aSecondBlock with: anotherBoolean with: aThirdBlock
You can use the #caseOf:otherwise:
message.
Look for some sender of this message for some example.
But anyway if you want to use case statement you are not following the smalltalk-way-of-doing-things. Tell us what you want to achieve with your case statement so that we can show you some cleaner way to do it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With