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

Jarrod Roberson jarrod@REDACTED
Wed Apr 30 21:22:17 CEST 2008


On Wed, Apr 30, 2008 at 1:55 PM, Jarrod Roberson <jarrod@REDACTED>
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.*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080430/0f954d9f/attachment.htm>


More information about the erlang-questions mailing list