Am Donnerstag, den 09.02.2017, 18:19 -0500 schrieb Steffen Smolka:
Thanks for the detailed answer, Jeremy!
If you're keen to stick with objects
Yes, I rely on inheritance and dynamic dispatch for what I have in mind. (This is actually the first time I'm touching the dark object oriented side of OCaml :) )
To give some more context, I am refactoring some code that uses modules and no objects. The reason I want to move to objects is that I want to derive a slightly enhanced module from some base implementation. Inheritance + dynamic dispatch allow me to do so with very little trouble: I can simply overwrite a few methods from the base implementation.
I suppose I could achieve the same by turning the base module into a functor, and abstracting over the functions that my enhanced implementation needs to replace. I think it won't be quite as natural, but I'll give that a try.
First-class modules could also be an option: Let's assume both the base module and the modified one can use the same module type:
module T = sig ... end
Now, define the base module like
module Base : T =
...
end
then, define the modified one:
module Mod : T =
include Base
... now override what you need to change but note that there's no dynamic dispatch ...
end
Of course, you could also use functors for making these modules.
Now turn this into first-class modules and pass them around:
let base = (module Base : T)
let mod = (module Mod : T)
The syntax for unpacking the module is quite cumbersome:
let module M = (val base : T) in
M.function ...
Unfortunately, there's nothing simple like base.function.
Compared with objects you get:
- You can also put types and (to some degree) modules into these "code containers"
- However, there's no dynamic dispatch except you arrange explicitly for that, e.g. with references to functions
- Generally, a heavier syntax, but it might be ok
Gerd
Or you could select the encoding using a variant type:
Good idea, and I'm happy with the syntax for the caller. But I'm more concerned with the organization of the code; this would mix the Latin1 and Utf8 implementations. I would rather keep them separate.
-- Steffen
--
------------------------------------------------------------
Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de
My OCaml site: http://www.camlcity.org
Contact details: http://www.camlcity.org/contact.html
Company homepage: http://www.gerd-stolpmann.de
------------------------------------------------------------