[erlang-questions] Parameterized module initialization
Loïc Hoguin
essen@REDACTED
Thu Jun 28 13:17:58 CEST 2012
On 06/28/2012 12:53 PM, Ulf Wiger wrote:
> On 28 Jun 2012, at 11:23, Loïc Hoguin wrote:
>
>> Second, you actually make things more confusing. See this small snippet:
>>
>> X = {utf8_string, 1, 2, 3},
>> X:some_function() %% this calls utf8_string:some_function/1
>>
>> Y = utf8_string,
>> Y:some_function() %% this calls utf8_string:some_function/0
>>
>> This means that you need to know what's in your variable to know the function that will be called. It's fine if your application is small and you know every bits of it. But for bigger systems where these variables might be set in different applications or different nodes, you never know what you'll get. This makes things incredibly hard to read and debug.
>
> Well, this is just as true for funs, and basically also for gen_servers,
> where the results of your function calls depend on what state the
> server has - which is actually worse in many ways, since the code
> is no longer referentially transparent, and thus much, much more
> difficult to test.
The result changes depending on state, sure. But gen_server always call
the same functions. Plus, gen_server calls are abstracted through an
API. So you always call the same function and you may get the same or
different result.
But I'm not talking about the result. I'm talking about how the same
code can call different arities depending on the contents of a variable.
Var:func() doesn't always call a function of arity 0, as you'd read it
instinctively, you need to look elsewhere to be sure of what is really
happening. The syntax Var:func() has two meanings instead of just the
one we expect.
--
Loïc Hoguin
Erlang Cowboy
Nine Nines
More information about the erlang-questions
mailing list