开发者

If you have Traits, do you stop using interfaces, Abstract base classes, and multiple inheritance?

开发者 https://www.devze.com 2023-03-23 10:25 出处:网络
It seems like Traits could completely replace interfaces, abstract base classes, mixins, and multiple inheritance, leaving you with just Traits and concrete inheritance.

It seems like Traits could completely replace interfaces, abstract base classes, mixins, and multiple inheritance, leaving you with just Traits and concrete inheritance.

Is this the intent?

If y开发者_C百科ou have traits, which of the other code structuring constructs should you use?

(Roles are the Perl name for Traits.)


At least for Perl's Moose, there are no interfaces, so roles clearly subsume those, and generally mixins too. I'd say there still could be a case for abstract base classes. Roles can be considered what objects do, where classes are what they are.

By this line of reasoning, there still might be a valid use for an abstract base class. A URL is one example. There could easily be an abstract base class for a URL. An IO stream might be different, perhaps better as a role, as it defines how things behave rather than what they are.

When using roles, however, I have yet to see any clear need for true multiple inheritance from more than one class.


I have no use for interfaces or abstract classes at this point, but mixins and multiple inheritance are really enabled by traits so the usage of those paradigms is strongly encouraged here. Check the entire collection library to see the very rich classes you can build using these ideas.

Ah, my comments reflect Scala - I didn't realize you tagged this with multiple languages.


When you instanciate a trait; it consumes one classe. So regardless of expressivity; You may still use legacy construct for preventing classes explosion in your jar (and starting time).

I let others answer about expressivity :)


I'm only talking about Scala here...

Read this.

0

精彩评论

暂无评论...
验证码 换一张
取 消