Suppose you have a class Foo, and that you want to be able to multiply a Foo by a Number to get another Foo, using ‘@@‘ as the multiplication sign.
Since multiplication is associative, it would be nice to be able to write:
| f a b |
f := Foo new.
a := 3 @@ f.
b := f @@ 3.
self assert: a = b
This requires not just adding the binary method “@@” to Foo, but also to the Number class. So you end up with essentially the same method in two different places (along with a circular dependency), which seems rather inelegant.
So I'm wonding, in Smalltalk, is there any other way to create associative binary messages where the sender and argument are of different types - one which doesn’t require you to define the same message in two different classes?
If not, is possible to create this ability using Smalltalk itself (ie. add classes/methods which automate the management of associative binary methods, without changing the actual SmallTalk language or VM)?
Aucun commentaire:
Enregistrer un commentaire