Re: why do i have to repeat so much here?
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <CAPFanBE__MT58T=-BUhjocNBh1UbQee2Zuq+Py+XcrRhjPDgmg@mail.gmail.com> |
You may be interested in this long caml-list thread about precisely this question: https://sympa.inria.fr/sympa/arc/caml-list/2012-10/msg00205.html The short answer is "there is no well-understood way to do better without design hacks". Some people have designed meta-programming tools to macro-away some of the boilerplate (eg. recently https://github.com/whitequark/ppx_import ). I would recommend first trying to do it the usual way, to get a feeling of how sufferable it actually is. On Tue, Aug 19, 2014 at 7:50 AM, Martin DeMello [email protected] [ocaml_beginners] <[email protected]> wrote: > > > I'm learning my way around the module system, and trying to write a small > Environment module that creates a record based on a given type. My code > works, but it looks like i had to essentially write out the record > definition thrice. Is there a better way to do this? > > Here's the definition of the module > > /-------------------------------- > $ cat environment.mli > > module type ENV = sig > type dict > > type env = { > dict : dict; > op : Types.uop > } > end > > module Make (Dict : sig type t end) : ENV with type dict = Dict.t > > $ cat environment.ml > > module type ENV = sig > type dict > > type env = { > dict : dict; > op : Types.uop > } > end > > module Make (Dict : sig type t end) = struct > type dict = Dict.t > > type env = { > dict : dict; > op : Types.uop > } > end > > \-------------------------------- > > and I'm using it in the following code: > > /-------------------------------- > module Evaluator = > functor (Env : ENV) -> > functor (E : ENGINE with type dict = Env.dict) -> > struct ... end > > module Env = Environment.Make (Trie) > > module Eval = Evaluator.Evaluator (Env) (TrieEngine) > \-------------------------------- > > martin > > > > >