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
RE: [Caml-list] Width subtyping
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-05-29 (15:38)
From: Dario Teixeira <darioteixeira@y...>
Subject: RE: [Caml-list] Width subtyping


> This way you're always dealing with the same product type,
> but the sum type tells you which fields are actually valid.
> Of course, it relies on there being an obvious sentinel
> value for unused fields (otherwise you just end up with 'a
> option everywhere) and I still don't think it's that neat as
> you can still match against fields which aren't valid but at
> least the type system prevents you from being handed an
> illegal value. The benefit is that you don't need special
> "get" functions (just a match on type t to extract the t'
> value from each constructor). I can't get my head around how
> private polymorphic variants work to see if they can refine
> this further...

This is indeed a reasonable solution for many classes of problems.
However, the need for a sentinel value for unused fields makes it
somewhat heavyweight for those record variants where just one field
is valid.  Moreover, this solution requires outside functions that
should only operate on T1 (for example) to "voluntarily" check by
pattern matching that the t they are getting is indeed a T1 (and
presumably raise failure otherwise).  The object-based solution can
at least automatically take care of this (though it has other problems).