does squeak have an equivalent of #askFor:

goran at krampe.se goran at krampe.se
Fri Aug 11 20:40:27 UTC 2006


Hi!

Keith Hodges <keith_hodges at yahoo.co.uk> wrote:
> I remeber a while back using #askFor:
> 
> which does something like.
> ------------
> askFor: selector
> 
> ^(self respondsTo: selector) ifTrue: [ self perform: selector ] ifFalse: 
> [ false ].
> ------------
> 
> is used for testing isThis and isThat's
> 
> a askFor: #isMyclass
> 
> best regards
> 
> Keith

It seems to me that there is a much simpler pattern to use if you want
to be able to ask an object if it "is" something.
The normal approach is to let all classes that think they are bananas
implement isBanana as "^true" and then let all others implement isBanana
as "^false". The above technique only has one advantage and that is that
you do not need to let all others implement isBanana, but this is
Smalltalk - it does not hurt to let them. :)

Depending on if you know a little bit about what classes are possible -
you can put the "^false" implementation(s) in different places. Perhaps
you have some abstract base class(es) that you can put it in. Or if you
really want to be able to test *any* object - put it in the class
Object. Using PackageInfo/Monticello you can easily mark that method as
an extension method that still belongs to your Monticello package. And
of course this is much faster than using respondsTo: and perform:.

But of course, I too use repondsTo: and perform: now and then - so I am
not saying it is forbidden in general.

regards, Göran



More information about the Squeak-dev mailing list