On 22/04/16 17:04, Hilaire wrote:
Hi Julien,
Le 22/04/2016 16:45, Julien Delplanque a écrit :
Hello,
I have a question about Trait.
Is it ok, in the design point of view, to use them to force an object to implement an interface? Why not using a parent abstract class for that, with required method answering #subclassResponsability? Yes this is what I do for now. But what if I do not want to force the use of inheritance?
Then from your tests in subclasses, you check
self shouldnt: [instance toto] raise: SubclassResponsabilty
This wont work if the object does not implement #toto at all.
Like that it would be possible to check (via an #assert: for example) if a class provides the interface required.
I read some things I found on the net about Traits and already used TComparable but I never created one myself and I am not sure to understand this concept entirely.
With Trait you add behavior to a class. TComparable adds behavior to your class, your instance are then comparable. Is it a problem? Isn't it similar to Java's interface? I see traits as a mix between Java's abstract classes and Java's interface since you can define methods and let some of them abstract but you can not define instance variables. But maybe I am wrong, that is why I ask :)...
Julien