New idiom for conditional testing?

danielv at netvision.net.il danielv at netvision.net.il
Sat Sep 29 17:25:15 UTC 2001


merlyn at stonehenge.com (Randal L. Schwartz) wrote:
>
> Well, rather than alter Collection for this one-off,
Well, I'm using the new and improved (read: I just patched it...) finder
tool to look for all occurances of ``@exp and: [``@exp2 and: [``@exp3] 
And no, I didn't design that syntax, I inherited from the RB parse tree
matching tools.

Anyway, I already found three occurances in just that part of the RB.
It'd be intersting how many occurances in the image.
(The search just returned - I stopped counting after 20, probably 50
more occurances. I think we can safely say deeply nested #and:s are an
idiom)

> you could
> keep it all here with:
> 
> {[self class = aRenameVariableChange class].
>  [className = aRenameVariableChange changeClassName].
>  [isMeta = aRenameVariableChange isMeta].
>  [oldName = aRenameVariableChange oldName].
>  [newName = aRenameVariableChange newName]}
>     do: [:e | e value ifFalse: [^false]].
>  ^true
Well, it's a nicer implementation than mine, but still - either it's
abstracted away, or it's not worth talking about...

> But the regularity of the pattern still irks me.  You're still
> faster cutting and pasting some of that code than simply typing it,
> which is always a red flag for me.  
Also true, but that's another pattern than bugged me - I don't like what
nested conditions in general look like. Alway annoys me when I'm writing
complex Celeste filters, for example.

Markus Gaelli <gaelli at emergent.de> wrote:
> But I saw this before, so why not extracting it:
> 
>     Object >> isEqualTo: anObject inAspects: someSymbols
>     ^someAspects allSatisfy: [:aSymbol |
>         (self perform: aSymbol) =
>         (anObject perform: aSymbol)]
> 
> And we would get:
> 
>     RenameVariableChange>>= aRenameVariableChange
>     ^self 
>         isEqualTo: anObject
>         inAspects: #(#class #className #isMeta #oldName #newName )
> Best

Aha! I like it! 
>From a quick browse of the various (80 in my image) implementations of
#=, about 75% could use this method. Some of those would need to stay
ugly for speed, probably. Anything that often lives in hashed
collections, anyway.

[strangely named method causes trouble]
No it doesnt - can call the changeClassName for the reciever too.

So we can do what you propose anyway, with #(class changeClassName
isMeta oldName newName ). All that VisualWorks is showing, Markus
(Squeak doesn't require the extra $#s) ;-)

Daniel




More information about the Squeak-dev mailing list