* restricting and hiding record fields for functors
@ 2008-01-04 8:24 Jeff Shaw
2008-01-04 10:09 ` [Caml-list] " Keiko Nakata
0 siblings, 1 reply; 2+ messages in thread
From: Jeff Shaw @ 2008-01-04 8:24 UTC (permalink / raw)
To: caml-list
Hello. I searched through the Google archives of this list, so I
apologize if I missed something relevant.
I'm working on a project that uses record types, perhaps
module type Cordinate = sig
type ordinate
type point2d = {x : ordinate; y : ordinate;}
val distance : point2d -> float
end
Now I have a functor over modules that match the signature Coordinate,
but I want to allow the modules given to the functor to have things in
the record "point2d" besides just x and y. It seems the types in this
case are really clear and there shouldn't be any ambiguity (but I'm no
expert).
As it is now, to use such records I have to entirely hide their contents
so that the type checker doesn't complain when "point2d" contains extra
fields. In addition, any fields I need access to in the functor will
have an associated function for setting and getting the values. For
instance,
module type Cordinate1 = sig
type ordinate
type point2d
val setx : ordinal -> point2d -> point2d
val sety : ordinal -> point2d -> point2d
val makepoint : ordinal -> ordinal -> point2d
val getx : point2d -> ordinate
val gety : point2d -> ordinate
val getpoint : point2d -> ordinate * ordinate
val distance : point2d -> float
end
This is really quite ugly. I would rather be able to have x and y in the
signature and get rid of get/set functions. But then you can't have
extra fields. You can't, for instance, hide them via a signature.
Or perhaps I'm missing some simple technique? Please let me know.
Thanks,
Jeff
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] restricting and hiding record fields for functors
2008-01-04 8:24 restricting and hiding record fields for functors Jeff Shaw
@ 2008-01-04 10:09 ` Keiko Nakata
0 siblings, 0 replies; 2+ messages in thread
From: Keiko Nakata @ 2008-01-04 10:09 UTC (permalink / raw)
To: shawjef3; +Cc: caml-list
Hello.
Maybe private row types are useful to you;
see the Language extensions section of the OCaml manual:
http://caml.inria.fr/pub/docs/manual-ocaml/manual021.html
You can do something like this:
module M = struct
type t = < x:int; y:int>
let r = object method x = 1 method y = 2 end
end;;
module F(X:sig type t = private <x:int; ..> val r: t end) =
struct let x = X.r#x end;;
module FM = F(M);;
let x = FM.x;;
I often prefer to use a syntax extension,
found at http://www.math.nagoya-u.ac.jp/~garrigue/code/ocaml.html,
which allows me to write
let r = {| x = 1 method y = 2 |}
instead of
let r = object method x = 1 method y = 2 end
With best regards,
Keiko
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2008-01-04 10:10 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-04 8:24 restricting and hiding record fields for functors Jeff Shaw
2008-01-04 10:09 ` [Caml-list] " Keiko Nakata
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox