[ANSI-Smalltalk] Re: A list of interesting methods, ifNotNil:
Richard O'Keefe
ok at cs.otago.ac.nz
Fri Sep 26 04:43:04 BST 2008
On 25 Sep 2008, at 9:35 pm, Andres Valloud wrote:
>
> something ifNotNilDo: [:someObject | someObject doWhatever]
>
> Why is this actually useful? I am not saying it's not, but I am
> having trouble seeing the reason.
Because the receiver, while in some sense "known", is not
as a rule *named*.
My library has
Object >>bind: aBlock ^aBlock value: self
Object >>bindOwn: aBlock ^[aBlock value: self] ensure: [self
close]
(when you are stacking stream transformations like encodings and
compression, I don't know how I would manage without #bindOwn:)
and
<e> ifNotNil: [:x | <b>]
is equivalent to
<e> bind: [:x | x ifNotNil: [<b>]]
>
> and skip the block argument. One could say "ah but then this requires
> a temp var". However, expressions such as
>
> something blah ifNotNilDo: [:something | something doWhatever]
>
> are only possible to write without parentheses when "something blah"
> does not have keyword messages in it... which is something that I try
> to avoid as much as possible...
Parentheses aren't really relevant here.
It's having a name for the result.
Me, I've always considered it better style to do
name := expression.
name ifNotNil: [...].
More information about the ANSI-Smalltalk
mailing list