[erlang-questions] Parameterized module initialization
Anthony Ramine
n.oxyde@REDACTED
Tue Jun 26 18:18:22 CEST 2012
Le 26 juin 2012 à 16:56, Joe Armstrong a écrit :
> I like this mechanism. This way of hiding additional data is very flexible.
>
> I've used this in several programs, and the resulting code is easy to
> understand. This nice thing is that using this you can make very nice
> client APIs. For example, for string processing you might say
>
> Str = {ansi_string, "abc"}, or
> Str = {utf8_string, "u+1234"}
>
> And then when you call
>
> Str:substr(1,4)
>
> Get the correct behaviour depending upon the underlying string type ie
> either ansi_string:substr(1,4,{ansi_string,"abc"}) or
> utf8_sting:substr(1,4,{utf8_string, "u+1234"} ) will be called
>
> If you write a program where strings might be in latin1, or utf8 or
> something else you'd still
> have to tag the string (as in say {latin1, "abc"}) so you knew what
> the encoding was and still
> have to call different modules depending upon the tag. This is what
> the above mechanism does
> so it's very nice for hiding the mess in polymorphic interfaces.
>
> /Joe
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.
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.
Regards,
--
Anthony Ramine
More information about the erlang-questions
mailing list