Version française
Home     About     Download     Resources     Contact us    
Browse thread
Ocaml type with constraints?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Gordon Henriksen <gordonhenriksen@m...>
Subject: Re: [Caml-list] Ocaml type with constraints?
On Sep 21, 2008, at 18:01, Angela Zhu wrote:

> I want to define an OCaml type with constraints. For example:
>
> type item = Item of int * float;;
>
> If here this float type is for price of some item, and I want to  
> make sure it is positive. In other words, if x = (xi, xf) of type  
> item, I want to enforce, xf must >= 0.
>
> Is there a way to define OCaml type like this?

Private types allow this enforcement, although you must implement the  
checks at runtime for the reason previously mentioned.

— Gordon