[ENH] ifEmptyParam-md
Adam Spitz
adspitz at yahoo.ca
Fri Jan 30 18:54:13 UTC 2004
marcus at ira.uka.de wrote:
> Now there's a nice way of having ifEmpty: take both a 0-arg block
> (like ifNotNil:) and a 1-arg block (like ifNotNilDo:) by using
> valueWithPossibleArgs: to double-dispatch on the number of
> arguments in the block.
I've been playing with a more general version of this. I added a couple
of methods to Object:
if: condition then: trueClause else: falseClause
^ (condition valueWithPossibleArgs: {self})
ifTrue: [trueClause valueWithPossibleArgs: {self}]
ifFalse: [falseClause valueWithPossibleArgs: {self}]
if: condition then: result
^ self if: condition then: result else: [self]
I also added a #valueWithPossibleArgs: method to Symbol, so I can do
things like:
name := (FillInTheBlank request: 'What is your name?')
if: #isEmptyOrNil then: [^ self].
For the really common tests like #isNil and #notNil, I like having
shortcut methods like #ifNil: and #ifNotNilDo:, just because it saves a few keystrokes.
And maybe #isEmpty is common enough to warrant a shortcut method, too,
but personally, I'd probably just go for the general one.
I also like it that if we had Self's implicit-self syntax, this method
would also serve as a plain ordinary vanilla if-then-else statement.
Adam
More information about the Squeak-dev
mailing list
|