From: "David Allsopp" <dra-news@metastack.com>
To: "'David Rajchenbach-Teller'" <David.Teller@univ-orleans.fr>,
"'Grégoire Seux'" <kamaradclimber@gmail.com>
Cc: "'caml-list'" <caml-list@inria.fr>
Subject: RE: [Caml-list] define incompatible type
Date: Fri, 12 Feb 2010 10:59:02 -0000 [thread overview]
Message-ID: <00b001caabd2$63c57fd0$2b507f70$@romulus.metastack.com> (raw)
In-Reply-To: <8D169DE8-B24E-4A80-8D2D-3743B6EF9A7A@univ-orleans.fr>
David Rajchenbach-Teller wrote:
> Hi Grégoire,
> It's not directly possible in OCaml, but there are at least three methods
for doing what you
> want.
>
> The first one is to wrap your integers behind a constructor, e.g.
<snip>
You can also use (post OCaml 3.11.0) private types if you want to be able to
use the ID values as integers but only explicitly. While I'm sure that the
ocaml compiler eliminates calls to the identity function, I like the
elegance that the conversion from id to int is a type coercion instead of a
function call. In real world uses, chances are that of_int is a useful
function doing actual work and not the identity function!
module User :
sig
type id = private int
val of_int : int -> id
end =
struct
type id = int
let of_int x = x
end;;
let a = User.of_int 57
and b = User.of_int 57;;
a = b;; (* true *)
a = 57;; (* type error *)
(a :> int) = 57;; (* true *)
David
next prev parent reply other threads:[~2010-02-12 10:59 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-02-12 6:32 Grégoire Seux
2010-02-12 7:33 ` [Caml-list] " David Rajchenbach-Teller
2010-02-12 7:43 ` Grégoire Seux
2010-02-12 10:59 ` David Allsopp [this message]
2010-02-12 20:26 ` Guillaume Yziquel
2010-02-12 9:25 abau
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='00b001caabd2$63c57fd0$2b507f70$@romulus.metastack.com' \
--to=dra-news@metastack.com \
--cc=David.Teller@univ-orleans.fr \
--cc=caml-list@inria.fr \
--cc=kamaradclimber@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).