About Traits

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

About Traits

Julien Delplanque
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?

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.


Thanks in advance!

Julien


Reply | Threaded
Open this post in threaded view
|

Re: About Traits

HilaireFernandes
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?

Then from your tests in subclasses, you check

self shouldnt: [instance toto] raise: SubclassResponsabilty


> 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.

>
>
> Thanks in advance!
>
> Julien
>

--
Dr. Geo
http://drgeo.eu


Reply | Threaded
Open this post in threaded view
|

Re: About Traits

Julien Delplanque


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

Reply | Threaded
Open this post in threaded view
|

Re: About Traits

Stephan Eggermont-3
In reply to this post by HilaireFernandes
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?
>
> Then from your tests in subclasses, you check
>
> self shouldnt: [instance toto] raise: SubclassResponsabilty
>

That limits one to one interface. Why not use a test

testTheConformingImplementation
     self assert: ((AConformingImplementation selectors)
asOrderedCollection  addAll: (AnInterfaceClass selectors)) asSet =
     (AConformingImplementation selectors) asSet