Thanks for your reply Jon. There's still a point that's not
clear to me : if the A module in your example corresponds to a file
a.ml used in the making of some .cma library, is it necessary to create an auxiliary
submodule as follows :
(*contents of a.ml *)
module TemporarySubmoduleForImplementationHiding : sig
type t
end = struct
type t = int list
end
type t=TemporarySubmoduleForImplementationHiding.t
or is it possible to do it "directly" ?
> In OCaml, the objective is to write as little code as possible. The above
> example requires you to write out lots of constructors by hand.
You've convinced me that my example is not the right way to do things, but if
I actually did it, I wouldn't write them out by hand. I would ask OCaml to
do it for me !
let rec interval a b=if (a>b) then [] else a::(interval (a+1) b);;
let sec_interval=interval(0)(59) in
let s=String.concat("|")(List.map (function i->"Sec"^(string_of_int i)^" " ) sec_interval) in
print_string ("nnn"^s^"nnn");;
and then a copy&paste would do the job ...
Ewan
--- In ocaml_beginners%40yahoogroups.com">ocaml_beginners
yahoogroups.com, Jon Harrop <jon
...> wrote:
>
> On Sunday 27 May 2007 07:04:00 roparzhhemon wrote:
> > Just how do you implement this "private datatype" in OCaml ?
>
> # type t = private A;;
> type t = private A
>
> > E.g, for seconds, one could write
> >
> > type number_of_seconds=Sec0 |Sec1 |Sec2 |Sec3 (...) |Sec59;;
> >
> > Is this good or bad practice in OCaml
>
> In OCaml, the objective is to write as little code as possible. The above
> example requires you to write out lots of constructors by hand.
>
> A shorter way is:
>
> type secs = Sec of int
>
> > Another related question : suppose I'm creating an OCaml library,
> > and I have a type int_set that is in fact an int list, but I don't want the
> > end user to know how it's implemented.
> > If I just write type t=int list in file "int_set.ml", the end user only
> > has to write "module X=Int_set;;" in the toplevel to learn that
> > Int_set.t is simply int list. Is there a way to hide the implementation ?
>
> Yes indeed. If you consider your module written with a full interface (you can
> get this by compiling the module structure with -i to get the inferred
> signature):
>
> module A : sig
> type t = int list
> end = struct
> type t = int list
> end
>
> All you do is leave off the definition of the type in the signature and it is
> abstracted away:
>
> module A : sig
> type t
> end = struct
> type t = int list
> end
>
> Everything outside the module A is then unaware that A.t is actually an int
> list.
>
> --
> Dr Jon D Harrop, Flying Frog Consultancy Ltd.
> The F#.NET Journal
> http://www.ffconsultancy.com/products/fsharp_journal/?e
>
.