[erlang-questions] Modules and function "encapsulation"
Wed Sep 1 05:57:24 CEST 2010
On 01/09/10 12:16, Silas Silva wrote:
> Hello all.
> I'm a newbie in Erlang and functional programming. Just got amazed to
> see how fast I could make a MVC application in Erlang, using its great
> pattern matching capabilities (not present in mainstream imperative
> languages) and other features.
> A doubt regarding modules, though, is in my mind:
> In my MVC application, the Model (for now, an interface to a Mnesia
> database) has the start/0 (exported) function. The Controller (which
> imports Model's start/0) also defines its own start/0 function. When
> trying to compile the Controller, I got the following error:
> defining imported function start/0
> I decided to make some research on my favourite search engine and found
> this thread on an Erlang forum:
> There, people talks about overloading. I can have start/0 in one module
> and start/1, start/2 etc. in another module, which "extends" the start
> function, but I can't have two start/0 functions. That is okay, but I
> thought that start/0 for a module would be totally different from
> start/0 from another module and both would not conflict. So, why there
> is the module prefixing syntax (controller:start()), -import and
> How do you deal with that? You just make functions name unique among
> all modules? This approach looks like in a big C program (e. g.: OS
> monolithic kernel), where symbols cannot conflict because, even though
> they are compiled in different .o files, the linker complains when
> putting everything together.
> Any recommendation?
Generally, you shouldn't use the -import statement. Call functions from
other models explicitly using the model:function syntax (such as
controller:start()). Using -import makes the code harder to follow and,
as you discovered, leads to name clashes.
More information about the erlang-questions