caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Alain Frisch <frisch@clipper.ens.fr>
To: Martin Jambon <m.jambon@ibcp.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Type aliases
Date: Thu, 28 Nov 2002 19:51:33 +0100 (MET)	[thread overview]
Message-ID: <Pine.SOL.4.44.0211281945550.16772-100000@clipper.ens.fr> (raw)
In-Reply-To: <Pine.LNX.4.40.0211281805450.1077-100000@pc-bioinfo1.ibcp.fr>

On Thu, 28 Nov 2002, Martin Jambon wrote:

> 1) Define type aliases:
...
> 2) Don't hide their representation and derive the primitives from the
>    original type:
...
> 3) Prohibit the mixing of different types that do not derive from each
>    other, without an explicit cast:
...
> Basically, I would like to minimize the risk of error when I manipulate
> simple datatypes like numerical parameters or int/string identifiers.

You can get something close to what you want with:

module Ints :
sig
  type 'a integer

  val int: int -> 'a integer
  val get: 'a integer -> int

  val ( + ): 'a integer -> 'a integer -> 'a integer
  val ( * ): 'a integer -> 'a integer -> 'a integer
(* etc... *)
end =
struct
  type 'a integer = int
  let int x = x
  let get x = x
  include Pervasives
end


integer literals must be written like (int 42), which is somewhat
painful, but Camlp4 can help here...

The point is that you cannot mix Apples and Oranges:

# open Ints;;
# let x : [ `Apple ] integer = int 3;;
val x : [ `Apple] Ints.integer = <abstr>
# let y : [ `Orange ] integer = int 4;;
val y : [ `Orange] Ints.integer = <abstr>
# get (x + y);;
This expression has type [ `Orange] Ints.integer but is here used with
type [ `Apple] Ints.integer


Hope this helps.


-- Alain

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2002-11-28 18:51 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-11-28 18:01 Martin Jambon
2002-11-28 18:51 ` Alain Frisch [this message]
2002-11-29 14:03   ` Martin Jambon
2002-11-28 18:55 ` Alessandro Baretta

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=Pine.SOL.4.44.0211281945550.16772-100000@clipper.ens.fr \
    --to=frisch@clipper.ens.fr \
    --cc=caml-list@inria.fr \
    --cc=m.jambon@ibcp.fr \
    /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).