3 ms·
Traits go a bit beyond generics, in my view. You can say i'm a "smug Lisp coder", but after having used CLOS (Common Lisp Object System), i'd say that generics
by flavio81 9y ago
Traits go a bit beyond generics, in my view. You can say i'm a "smug Lisp coder", but after having used CLOS (Common Lisp Object System), i'd say that generics are one of the most useful things you can have in OOP.
With Traits you can separate implementation of certain methods (implementation of Traits) from object definitions. Which for me is excellent, and approaches CLOS' way (in CLOS methods are not directly coupled to a particular object; methods are applied to the combination of one (or more) object(s).
In a similar way as English language; where you apply an action involving many subects: "Angel transport his luggage using a car." Here the 'transport' action can't really be bound to the Person class, nor the Luggage class. Perhaps it belongs on the Car class, but with CLOS you can just define "transport" to act on the combination of <actor> <object to be transported> and <vehicle>.
"Transport", thus, is a "generic method" and can be defined (implemented) for all kinds of class combinations.
With Traits you can achieve something more or less similar, by defining the necessary Traits for each kind of class, to be able to have all of them "transport" harmoniously. So Traits can link (and then impement) the operations performed between classes, without having to bound the code to a particular class.
- lispm 9y agoCLOS: A combination of applicable methods from a generic function is applied to objects.