- One example:
module Make (Arg : S) = struct (** ...Use Arg at will... *) let id = ref 0 let id () = incr id; !id end
Each instantiation resultMake(Arg)
will have it's own id generator. - If you don't feel you need it, just don't do it. In general having state makes reasoning about your code harder.
Domanda
In Real World OCaml, Chapter 9 Functors, it says
Instantiating modules with state
Modules can contain mutable states, and that means that you'll occasionally want to have multiple instantiations of a particular module, each with its own separate and independent mutable state. Functors let you automate the construction of such modules.
The book doesn't have much content on this sub topic. So I ask here.
Can anyone give me an example of instantiating modules with state to demonstrate
How to do that?
When to do that?
Soluzione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow