Re: why do i have to repeat so much here?

"Philippe Veber [email protected] [ocaml_beginners]" <[email protected]>
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <CAOOOohRPcrUroP2hC0YWGuMjb6jRKY_h24HjYGe91jBwWCjQfA@mail.gmail.com>
Hi Martin,

A frequently used trick is to define your module type in an ml file to
avoid one repetition:

environment_sig.ml:

module type ENV = sig
  type dict

  type env = {
    dict : dict;
    op : Types.uop
  }
end

environment.mli:

module Make (Dict : sig type t end) : Environment_sig.ENV with type dict =
Dict.t


environment.ml:

module Make (Dict : sig type t end) = struct
  type dict = Dict.t

  type env = {
    dict : dict;
    op : Types.uop
  }
end

Cheers,
  Philippe




2014-08-19 8:22 GMT+02:00 Martin DeMello [email protected]
[ocaml_beginners] <[email protected]>:

>
>
> interesting thread. i usually don't mind repeating myself in the .mli file
> - i think what is annoying me the most here is that i don't really have an
> "implementation"; all i want to do is define a single record type with a
> type parameter. i know i could delete the .mli file and have everything
> work, but that feels wrong when theoretically i want to delete the .ml file
> instead.
>
> thanks for the pointer to ppx_import - at some point i'll probably get
> annoyed enough to use it :) though as you advise, i'll maintain the current
> code for a while first, and see how painful it is to add fields to the
> record in three places every time.
>
> martin
>
>
> On Mon, Aug 18, 2014 at 10:54 PM, Gabriel Scherer
> [email protected] [ocaml_beginners] <
> [email protected]> wrote:
>
>>
>>
>> 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
>>>
>>>
>>>
>>>
>>
>
>
> 
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.