[ANSI-Smalltalk] Re: A list of interesting methods, ifNotNil:
Paolo Bonzini
bonzini at gnu.org
Mon Sep 8 12:26:39 BST 2008
> I am now confused.
> The actual code in Squeak is
>
> ifNotEmptyDo: aBlock
> ^self isEmpty ifTrue: [aBlock value: self]
>
> It certainly doesn't involve any sending of the message #do:.
Which is all but intention revealing.
> x isEmpty ifTrue: [x do: monadicBlock]
>
> would, if x is a collection, never do anything at all with monadicBlock.
> I take it that this should have been
>
> x isEmpty ifFalse: [x do: monadicBlock]
>
> and there is a name for that too: #do:.
Of course, but there is no existing name for #ifEmpty:ifNotEmptyDo:.
Moreover, if you have another message #ifEmpty:ifNotEmpty: there is no
hint that the two differ *in the arity of the second block*!
If there were to be methods like #ifEmpty:, you should have only these four:
#ifEmpty:
#ifNotEmpty:
#ifEmpty:ifNotEmpty:
#ifNotEmpty:ifEmpty:
plus possibly
#ifEmpty:ifNotEmptyDo: (passing the second block to #do:)
#ifNotEmptyDo:ifEmpty:
Furthermore, all parameters of all six methods should have variable
arity, because unlike the nil/not-nil case, there are many empty
collections and you may want to know it even in the empty case. I agree
that this is borderline -- I cannot think of a use case indeed, and most
of the time you'll use #ifEmpty: just as a guard for ^self.
> VisualWorks also has an if*Do: method or two.
> GeneralBindingReference>>ifDefinedDo:
> has nothing to do with #do:.
> WindowManager>>activeControllerProcessIfBlockedDo:
> also has nothing to do with #do:.
But no #ifDefined: and #activeControllerProcessIfBlocked:. I don't like
those names very much, but that's nothing compared to overloading "Do:"
(which already has a well-known meaning in Smalltalk, i.e. "iterate") to
mark arity.
Paolo
More information about the ANSI-Smalltalk
mailing list