Version française
Home     About     Download     Resources     Contact us    
Browse thread
Attach an invariant to a type
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Edgar Friendly <thelema314@g...>
Subject: Re: [Caml-list] Attach an invariant to a type
Dawid Toton wrote:
>> I guess that making types like "int" private would require the system
>> (among other things) to decide whether you're using a given integer as
>> a Subindex.t or not.
> 
> Suppose we have "type t = private int" in module Subindex.
> 
> Is the following construct legal: let (a:Subindex.t) = 2 ?
> It shouldn't be - to prevent me from constructing values of this type.
> So I couldn't "use given integer as Subinddex.t".
> 
type t = private int isn't legal.  Most types get completely erased
during compilation, but records and variants have code generated by the
compiler based off their type declaration to construct a value of that
type.  A plain int doesn't have this compiler-generated constructor, so
it can't be private in this way.

That said, I'd appreciate a simple system to do the kind of checking you
want at the site of an explicit typecast.

E