Erlang/OTP Forums

Author Message

<  Open Telecom Platform (OTP)  ~  exporting functions

hermanns
Posted: Tue Sep 12, 2006 7:07 pm Reply with quote
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
View user's profile Send private message
diginux
Posted: Tue Sep 12, 2006 7:41 pm Reply with quote
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.
View user's profile Send private message
hermanns
Posted: Tue Sep 12, 2006 9:15 pm Reply with quote
Joined: 12 Sep 2006 Posts: 2
But spawning counter:loop/1 happens inside the module.

I think there must be another explanation.
View user's profile Send private message
garazdawi
Posted: Wed Sep 13, 2006 8:26 am Reply with quote
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
View user's profile Send private message
vladdu
Posted: Thu Sep 14, 2006 10:49 am Reply with quote
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
View user's profile Send private message
vladdu
Posted: Thu Sep 14, 2006 10:51 am Reply with quote
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
View user's profile Send private message
francesco
Posted: Fri Sep 15, 2006 5:37 pm Reply with quote
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
View user's profile Send private message Visit poster's website
vladdu
Posted: Fri Sep 15, 2006 6:37 pm Reply with quote
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 Smile 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
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum