Does standard UML specify how a variant (aka tagged union, discriminated union, sum type, etc) should be depicted in a class diagram?

  • It's going to be such a shame if it doesn't. – Trident D'Gao Nov 18 '14 at 15:02
  • What type of diagram are you attempting to model a tagged union in? I'm guessing a class diagram? – Thomas Owens Nov 18 '14 at 15:08
  • Good catch, yes a class diagram (updated) – Trident D'Gao Nov 18 '14 at 15:25
  • You can always model it the way it is usually implemented (e.g. MSDN: VARIANT structure) or as en.wikipedia.org/wiki/…. It is such a shame that your unclear question with no concrete examples given probably does not have an useful answer – xmojmr Nov 18 '14 at 15:42
  • 1
    @xmojmr, are you asking for an example of a variant? here it is in F# flavor: type Option<'a> = | Some of 'a| None, in haskell flavor: data Maybe a = Nothing | Just a. I am sorry if the notion of a variant is unfamiliar to you. What else is unclear? Do you need an example of a class? – Trident D'Gao Nov 18 '14 at 15:49

The Unified Modeling Language is not so much unifying programming paradigms. It is dramatically focused towards object-oriented programming (hence, f.ex. its most popular diagram being the class diagram). In contrast, sum types are typically a feature of functional languages (like F#, Haskell, etc.).

I'm afraid to inform you that UML is probably not a good choice to model programs that will be developed with a functional language. A huge bag of their features do not have direct correspondences in UML unfortunately. Features that form the bread-and-butter of FP, like first-class functions, type constructors, etc., are troublesome to represent in UML, or flat out impossible.

If you must represent sum types, then you could model a representation that is close to their implementation in Scala, where you actually define a base class/trait/interface and derive the individual sum types. Something like this (simplified):

sealed trait Option[+A]
case object None extends Option[Nothing]
case class Some(value : T) extends Option[T]

Of course, you will fall short of modelling this in several aspects (trait? case class? covariance? sealed?), but that is my general experience when you try to model non-OOP features in UML.


A classifier in UML can encompass multiple subclassifiers that form something they call a "generalization set" which can be a complete partitioning of the instances of a superclass. There's a graphical notation for this in class diagrams. So, you can have a classifier C that has subclasses P Q R ... with the property that any instance of C must be an instance of exactly one of P Q R ... That is the closest I've been able to come to a UML visualization of union. OOP folk seem to think of the superclass "coming first" and then deriving the subclasses, which is not quite the same "feel" as the union perspective which is about defining C in terms of P Q R. But if you think of the UML class diagram as describing possible run-time object graphs rather than picturing source code, the generalization set with a superclass that has no attributes or operations may be close to what you want.

UML class diagrams are really inadequate to visually depict basic logical relationships like disjunction, etc


Composition could likely be used to denote tagged unions in a UML class diagram. Composition is the "has a" relationship and a tagged union has a tag and a value. You can use multiplicity and role names or ownership indicators.

However, something to consider is that class diagrams are typically geared toward representing class relationships in an object-oriented language. Other diagram types may be less specific to object-orientation, such as deployment diagrams or component diagrams. You may want to consider other notations beyond just using UML and emphasize clearly communicating a design over sticking to one standard.

  • using a tag and a value is just a design pattern that gives you something that works like a variant, in languages that natively support it there is no need in knowing how a variant is internally implemented, a variant is a first class citizen with its own distinct syntax that cannot be expressed using other constructs like classes, structures, etc – Trident D'Gao Nov 18 '14 at 15:53
  • which means there should be a special notation in UML reserved for it, just like for a class or an interface, I just wasn't able to find it – Trident D'Gao Nov 18 '14 at 15:56
  • 1
    @bonomo What language are you using? I've implemented a tagged union in C++, and if I had to represent it, I'd use composition. – Thomas Owens Nov 18 '14 at 15:59
  • I use TypeScript, F# and Haskell. But the question can be extended to any language that supports algebraic data types – Trident D'Gao Nov 18 '14 at 16:12
  • 1
    @bonomo Can you update your question with a sample class diagram that you have? I'm not familiar with TypeScript, but I think my second paragraph applies. You may want to consider something other than UML class diagrams to model functional languages like F# and Haskell - I know I wouldn't use them because that's not what they are designed to do. – Thomas Owens Nov 18 '14 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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