[bitc-dev] Unions, classes, and syntax
Jonathan S. Shapiro
shap at eros-os.org
Thu Apr 22 07:54:17 PDT 2010
We're almost certainly going to take on single inheritance at some
point, so I want to get straight in my head what is different between
unions and subclasses.
So far as I can tell, a union is best imagined as a supertype having
only a tag, and a set of subtypes that constitute the union legs. The
differences between unions and subclasses are:
1. Unions are "closed" - the set of subclasses is statically known.
2. In consequence, unions have a known static size. This allows the
semantics of unboxed assignment to be quite different. That is, given:
B <: A, C <: A
a location of type A can be overwritten with a value of type B or C if A is
a union, but can only be overwritten with a value of type A if A is
And yet, ref A can be assigned from ref B or ref C without loss, and in
particular, without loss of the vTable and the associated parts of the
The difference in unboxed assignment behavior makes me suspicious that
there may be more semantic difference between the two than I have
outlined, and that in consequence, sum types are not subsumed by
They are operationally similar enough that I think related syntax is
useful. In particular, a dynamic cast syntax:
e as T
can be used in either case.
Comments and feedback on either semantics or syntax here?
More information about the bitc-dev