Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Type aliases
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Alain Frisch <frisch@c...>
Subject: Re: [Caml-list] Type aliases
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