Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Is there a representation for modern programming constructs that are becoming common in modern OOP/hybrid languages like mixins/traits/categories/partial classes ?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Yes and no. Generally, UML allows the usage of multiple inheritance, which allows you to model something like traits. In a sense, traits in Scala are a way to allow multiple inheritance, whilst trying to reduce its negative effect (namely, the diamond inheritance problem).

As for the Yes part of the answer: you can show traits/mixins as normal classes in UML, or even add a corresponding stereotype to them (either make them abstract, or if you want to be more precise define your own profile with a proper trait-stereotype). A class that mixes multiple traits is then visualized as a class that inherits from these traits (via generalization associations).

However, the UML has no proper semantic concept of this inheritance. Other than a vague description that the generalization means that the subclass gets the features from its superclasses, you will not get a clean semantics. In particular, language-specific semantics like Scala's trait linearization are not directly supported in UML.

If you want to make a very precise model of your Scala traits/classes, you would need to define a custom UML profile and assign stereotypes to your generalizations to document their linearization order. However, that's limited as well, because the order may be different for different classes that share some of their traits.

So it depends on what you want to use UML for. If you want to use it as documentation, you may well be able to live with these shortcomings. If you want to use UML models as a base for code generation or some such, then newer language features spell trouble. While the UML offers semantic variation points it is as a whole based on the traditional OO languages and falls short as soon, as you leave these standard feature sets. While traits/mixins still relate closely to classes/objects, and hence, can be visualized with some trade-offs, you'll be even more at a loss, when you try to use UML for non-OO language aspects (think, functional or logic programming).

share|improve this answer
    
The thing is, it's not multiple inheritance. In MI, a class can have multiple superclasses, all of which appear exactly once in the inheritance graph. With traits/mixins, each class has exactly one superclass (or supertrait), but that trait can appear in multiple different places in the inheritance graph. A mixin/trait can be characterized as a class that is parameterized by its superclass, and UML does support parameterization of classes, so you could probably represent them that way. How, I have no idea. Someone with deep knowledge of UML should chime in. –  Jörg W Mittag Oct 30 '13 at 18:48
    
Yes I'm aware it is not exactly multiple inheritance (the question would be void then anwyays), but I still think it's valid to represent it as such, because the semantics are very close. Of course it is not exactly the semantics of trait linearization, but it's still closer than parameterization, which would not inherit the trait's features into the class. –  Frank Oct 31 '13 at 6:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.