Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] syntax of private constructors in CVS version
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: brogoff@s...
Subject: Re: [Caml-list] syntax of private constructors in CVS version
On Fri, 27 Jun 2003, Shaddin Doghmi wrote:
>  From the CVS version  changes file, under language features:
> 
> - Support for "private types", or more exactly concrete data types
>   with private constructors or labels.  These data types can be
>   de-structured normally in pattern matchings, but values of these
>   types cannot be constructed directly outside of their defining module.
> 
> What is the syntax for those? is there some kind of documentation on the 
> CVS server, like a development version of the manual or something?

No documents are available yet. Check out the following:

bpr@granite[bpr]$ ledit ~/bin/ocaml
        Objective Caml version 3.06+36 (2003-06-19)

# type t = private { x : int; y : int } ;;
type t = private { x : int; y : int; }
# type ('a, 'b) choice = private L of 'a | R of 'b;;
type ('a, 'b) choice = private L of 'a | R of 'b
# let p0 = { x = 0; y = 0 } ;;
One cannot create values of the private type t
# module Point2D : 
    sig 
      type t = private { x : int; y : int }
      val grid : int 
      val mkPoint : int -> int -> t 
    end = 
  struct
    type t = { x : int; y : int }
    let grid = 10 

    let snap i =
      let rem = i mod grid in
      if rem <= 0 then
	i - rem
      else
	i - rem + grid

    let mkPoint x y = { x = snap x; y = snap y } 
  end;;
module Point2D :
  sig
    type t = private { x : int; y : int; }
    val grid : int
    val mkPoint : int -> int -> t
  end
# open Point2D;;
# match mkPoint 5 5 with { x = x; y = y } -> Printf.printf "(%d, %d)\n" x y;;
(10, 10)
- : unit = ()

-- Brian

-------------------
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