[erlang-questions] what is the correct syntax to spawm a MFA with no arguments?

mog <>
Wed Apr 30 22:00:44 CEST 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jarrod Roberson wrote:
> On Wed, Apr 30, 2008 at 1:55 PM, Jarrod Roberson
> < <mailto:>> wrote:
>
>     start() -> spawn(fun loop/0).
>
>     I want to convert the above code snippet into a MFA, what is the
>     correct syntax to do this.
>
>     I tried start() -> spawn(?MODULE, fun loop/0, []). and all I get
>     when I call it is ** exception error: bad function
>     'area_server.start'
>
>
>
> I have also tried spawn(?MODULE, loop, []) and I get a warning that
> loop/0 is unused.
>
> 1> c(area_server).
> ./area_server.erl:16: Warning: function loop/0 is unused
> {ok,area_server}
> 2> P = area_server:start().
> <0.40.0>
>
> =ERROR REPORT==== 30-Apr-2008::15:20:53 ===
> Error in process <0.40.0> with exit value:
> {undef,[{area_server,loop,[]}]}
>
> 3>
>
> here is the entire program.
>
> -module(area_server).
> -export([start/0, area/2]).
>
> start() -> spawn(area_server, loop, []).
>
> area(Pid, What) ->
>     rpc(Pid, What).
>
> rpc(Pid, Request) ->
>     Pid ! {self(), Request},
>     receive
>         {Pid, Response} ->
>             Response
>     end.
>
> loop() ->
>     receive
>         {From, {rectangle, Width, Ht}} ->
>             From ! {self(), Width * Ht},
>             loop();
>         {From, {circle, R}} ->
>             From ! {self(), 3.14159 * R * R},
>             loop();
>         {From, Other} ->
>             From ! {self(), {error, Other}},
>             loop()
>     end.
>
> *What I really need to know is how to MFA spawn a process around a
> function with no arguments.*
>
> ----------------------------------------------------------------------
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
loop needs to be exported if your going to call it with spawn(mod,
fun, args) it does not need to be exported if you call spawn(fun()
function() end) , it would need to be exported if you called
spawn(fun() module:function() end)

mog
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFIGM/req+tARrxhnsRArPQAKCSwAoxyf9jVXX5njTo+j81RZF+WgCfXMsW
sBcM8fRm32RIBtDHHaHco5M=
=Whe7
-----END PGP SIGNATURE-----




More information about the erlang-questions mailing list