On Jul 30, 2007, at 6:52 PM, Marcel Weiher wrote:
> However, the fact that you need to use #perform: when you want to have
> a variable selector has bothered me for some time, because it is a
> weird asymmetry that, for example, functional programming languages do
> not seem to share: if you get passed a function (function pointer in
> C and the like), you can call it using natural syntax. So what I
> would like to see would be some sort of "selector variable", a
> variable that can be used in a standard message expression but is
> actually variable:
>
> | variableSelector:withArg: |
>
> variableSelector:withArg: := self computeSelector.
> " probably
> need to check arity for compatibility here "
> self variableSelector:'hello' withArg:2.
>
> Not sure this is workable/agreeable in a Smalltalk context, but
> something like it would be really nice. IMHO.
I think maybe the way to do this sort of thing is with the expanded
metaprogramming facilities that id provides. One of the more common
uses of #perform: is creating pluggable objects. Imagine a
PluggableSet - a Set that stores a selector and uses that for
equality comparisons. In Smalltalk there would be methods something
like this:
comparisonSelector: aSelector
comparisonSelector := aSelector
compare: anObject with: anotherObject
^ anObject perform: comparisonSelector with: anotherObject
Another way to do this would be to generate a class on the fly. So
something like this:
PluggableSet class>>new: aBlock
class := self class new.
class selectorAt: #compare:with: put: aBlock.
^ class new
You can't dispatch to a block in Smalltalk, and one-off classes are a
hassle because of all the conventions that they violate. It could be
elegantly done in id, though. PluggableObjects are not the only use
of #perform: of course, but ultimately the purpose of #perform: is to
bind behavior even later than usual - method execution time instead
of compile time. Id has more powerful metaprogramming capabilities
than Smalltalk, so in general there should be more elegant ways to
accomplish that than using #perform:.
Colin
_______________________________________________
fonc mailing list
[email protected]
http://vpri.org/mailman/listinfo/fonc