[erlang-questions] Parameterized module initialization
Richard Carlsson
carlsson.richard@REDACTED
Tue Jun 26 20:51:46 CEST 2012
On 06/26/2012 05:56 PM, Joe Armstrong wrote:
> 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
If you want to do it properly, Joe, then use the automatically generated
instantiator functions "new/N" instead of building tuple literals like that:
Str = ansi_string:new("abc"), or
Str = utf8_string:new("u+1234")
then the code will keep working even if module instances become a
separate data type (like funs), be easier to understand when you or
anyone else reads it later on, be possible to process using tools like
Dialyzer and Wrangler, and be easier to grep for "ansi_string:" etc.
/Richard
More information about the erlang-questions
mailing list