| Author |
Message |
|
| hermanns |
Posted: Tue Sep 12, 2006 7:07 pm |
|
|
|
Joined: 12 Sep 2006
Posts: 2
|
Code:
-module(counter).
-export([start/0, loop/1]).
start() -> spawn(counter, loop, [0]).
loop(Value) ->
receive
increment -> loop(Value + 1)
end.
Why do I have to expose the loop/1 function in the export statement?
IMO, this function should ideally not be part of the public interface, but when I remove it from the export statements, the programm does not work.
regards, Jan |
|
|
| Back to top |
|
| diginux |
Posted: Tue Sep 12, 2006 7:41 pm |
|
|
|
User
Joined: 08 Sep 2006
Posts: 13
|
hermanns wrote: Code:
-module(counter).
-export([start/0, loop/1]).
start() -> spawn(counter, loop, [0]).
loop(Value) ->
receive
increment -> loop(Value + 1)
end.
Why do I have to expose the loop/1 function in the export statement?
IMO, this function should ideally not be part of the public interface, but when I remove it from the export statements, the programm does not work.
regards, Jan
I am newish to erlang, and not completely sure, but I think it is because you are spawning counter:loop([0]), and the only way you could spawn that new process is if its exposed. When you spawn it is equivalent to as if you had run the function yourself from erl. |
|
|
| Back to top |
|
| hermanns |
Posted: Tue Sep 12, 2006 9:15 pm |
|
|
|
Joined: 12 Sep 2006
Posts: 2
|
But spawning counter:loop/1 happens inside the module.
I think there must be another explanation. |
|
|
| Back to top |
|
| garazdawi |
Posted: Wed Sep 13, 2006 8:26 am |
|
|
|
User
Joined: 10 Jul 2006
Posts: 20
|
Whenever you do a full functioncall ( i.e. Module:Function/Arity or MFA) the function you want to call has to be exported and as the only way to do a spawn is by doing a MFA call the function has to be exported.
Also your reasoning that as it is in the same module it should not have to be exported is flawed. If we would have been wroing with a non-concurrency oriented language you would most probably be right but in Erlang each process is a seperate entity and does not know what the process which spawned it was executing before. Therefore the function which the spawned process starts to should be exported. |
_________________ Erlang Training & Consulting |
|
| Back to top |
|
| vladdu |
Posted: Thu Sep 14, 2006 10:49 am |
|
|
|
User
Joined: 28 Feb 2005
Posts: 397
Location: Gothenburg, Sweden
|
hermanns wrote: Code:
start() -> spawn(counter, loop, [0]).
Why do I have to expose the loop/1 function in the export statement?
regards, Jan
Hi,
You can do this instead:
Code:
start() -> spawn(fun() -> loop(0) end).
regards,
Vlad |
|
|
| Back to top |
|
| vladdu |
Posted: Thu Sep 14, 2006 10:51 am |
|
|
|
User
Joined: 28 Feb 2005
Posts: 397
Location: Gothenburg, Sweden
|
hermanns wrote: Code:
start() -> spawn(counter, loop, [0]).
Why do I have to expose the loop/1 function in the export statement?
regards, Jan
Hi,
You can do this instead:
Code:
start() -> spawn(fun() -> loop(0) end).
regards,
Vlad |
|
|
| Back to top |
|
| francesco |
Posted: Fri Sep 15, 2006 5:37 pm |
|
|
|
User
Joined: 07 Jul 2006
Posts: 249
Location: London
|
I agree that exporting spawned functions is not good design. The reason, however, is probably historic, as the loop function was once called "from outside the module".
I am not sure spawn(fun() -> loop()) will work when doing a software upgrade. They might have solved the problem now (Did not follow up on it, as I never use it), but in the earlier releases, it did not work.
Francesco |
|
|
| Back to top |
|
| vladdu |
Posted: Fri Sep 15, 2006 6:37 pm |
|
|
|
User
Joined: 28 Feb 2005
Posts: 397
Location: Gothenburg, Sweden
|
francesco wrote:
I am not sure spawn(fun() -> loop()) will work when doing a software upgrade.
It should work fine, as long as loop/0 isn't the real loop That one has to be exported in order to be able to support code update. One might better say spawn(fun()->init_process()) and let the "real loop" be exported from a possibly not public module (i.e. not documented) or be a gen_server or something like that.
regards,
Vlad |
|
|
| Back to top |
|
|
|