Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Single-case union types as strong typedefs
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] Single-case union types as strong typedefs
From: Nathaniel Gray <>

> Since we're talking about degenerate cases (unit vs. void) I might as
> well ask a question that's been on my mind for a while.  There are
> many times when you have types that are structurally identical but
> conceptually different.  One example is lengths measured in different
> units, another is values measured in identical units that shouldn't be
> mixed unintentionally (e.g. widths and heights).  It would be really
> nice to be able to use a coding style like this (contrived) example
> that you could imagine coming from a graphics library:
> (* Library code: *)
> type relative_pos = Rpos of int * int
> type absolute_pos = Apos of int * int


> This is all legal right now, but the problem is performance since the
> tuples get boxed unnecessarily.  But non-polymorphic single-case union
> types can always be optimized away.

With your example, there is no extra boxing involved.
I.e. (1,3) and Rpos(1,3) and Apos(1,3) all share the same physical
representation (a block with 2 fields).

The problem you describe only occurs when your single constructor has
only one argument.
Indeed, this would be nice to have the overhead compiled away.
I don't remember whether there was a concrete reason not to.
At least one reason could be that if you add a new constructor later, you
will not be able to read dumped values of the previous type anymore,
but this doesn't seem a very strong reason.
Note that Haskell has a special notation for this case, using "newtype"
rather than "type" or "data", which marks explicit this as a special

Jacques Garrigue

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: