[erlang-questions] Parameterized module initialization

Richard Carlsson carlsson.richard@REDACTED
Tue Jun 26 20:32:22 CEST 2012


On 06/26/2012 07:18 PM, Anthony Ramine wrote:
> I think it is quite ugly to have terms as module parameters; a more
> beautiful way in my opinion would be to have something like Caml
> functors, which are mappings from structures (modules in Erlang) to
> structures.

Parameterized modules in Erlang already correspond to what ML/Caml calls 
"functors": an abstraction over modules. What you decide to parameterize 
over: terms or other modules (or possibly even types, though I'm not 
sure what would be needed to support that), is up to you.

> You would have a string functor which takes as a parameter a string
> implementation module, either ansi_string or utf8_string. You could
> then abstract the common stuff from ansi_ and utf8_ in string.

And that is just as straightforward to do in Erlang as in Caml:

-module(string, [StringImpl]).
-export([reverse/1]).
reverse(S) -> StringImpl:reverse(S)

and then you should probably hide the details of instantiating such a 
module behind some factory module or facade:

-module(string_maker).
-export([...]).
ansi_string_module() -> string:new(ansi_string).
utf8_string_module() -> string:new(utf8_string).


     /Richard



More information about the erlang-questions mailing list