Hi,
Say I'm writing some module M which, given data of some type t,
attaches extra information to that data, and has functions which
operate on both the data and the extra info. (My particular example
is that of a dependency graph: type t is some computation to be
performed, the extra info added by M is the dependency relations.) In
such a situation is it considered better style to (a) have the module
create an augmented data type, with a way for the user to extract
their data, such as:
module M = sig
type 'a t
val create: 'a -> 'a t
val do_stuff: 'a t -> unit
val get_data: 'a t -> 'a
end
or (b) have the user of the module provide room in their type for M's
extra info, and pass accessor functions to M, for example:
module M = sig
type t
val create: unit -> t
module type User = sig
type data
val get_m: data -> t
end
module Make(User: User) = sig
val do_stuff: User.data -> unit
end
end
As an example of the usage of the latter construction:
module My_data = struct
type data = { my_data: string; m_info: M.t }
let create x = { my_data = x; m_info = M.create () }
let get_m data = data.m_info
end
module My_M = M.Make(My_data)
The advantage I see in (b) is that the user remains in control of the
data structure, while with (a) the user must use M's data structure
and the get_data method. On the other hand, (a) is much simpler.
Thanks for you thoughts,
Chris King
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"ocaml-developer" group.
To post to this group, send email to [EMAIL PROTECTED]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/ocaml-developer?hl=en
For other OCaml forums, see http://caml.inria.fr/resources/forums.en.html
-~----------~----~----~----~------~----~------~--~---