简体   繁体   中英

Why is spawn() claiming my function does not exist?

I am working through the Learn You Some Erlang book and have the following code:

-module(fridge).

%% API
-export([start/1]).

start(FoodList) -> spawn(?MODULE, fridge2, [FoodList]).

fridge2(FoodList) ->
  receive
    {From, {store, _Food}} ->
      From ! {self(), ok},
      fridge2([_Food|FoodList]);

    {From, {take, Food}} ->
      case lists:member(Food, FoodList) of
        true ->
          From ! {self(), {ok, Food}},
          fridge2(lists:delete(Food, FoodList));

        false ->
          From ! {self(), not_found},
          fridge2(FoodList)
      end;

    {terminate} ->
      ok
  end.

However, when I try to call my start() function I get the following error:

36> c(fridge).
fridge.erl:8: Warning: function fridge2/1 is unused
{ok,fridge}
37> fridge:start([]).
<0.111.0>

=ERROR REPORT==== 10-Mar-2016::22:02:42 ===
Error in process <0.111.0> with exit value:
{undef,[{fridge,fridge2,[[]],[]}]}

What am I doing wrong, and why is it saying the function doesn't exist when it does?

spawn(Module, Function, Args) -> pid()

Returns the pid of a new process started by the application of Module:Function to Args.

So you have to export your function fridge2/1 like start/1 as an api.

orelse write like this:
start(FoodList) -> spawn(fun() -> fridge2(FoodList) end).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM