Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smalltalk Variadic functions

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
like image 475
Roman A. Taycher Avatar asked Oct 06 '10 12:10

Roman A. Taycher


2 Answers

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

like image 197
Frank Shearar Avatar answered Sep 24 '22 06:09

Frank Shearar


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.

like image 42
mathk Avatar answered Sep 25 '22 06:09

mathk