New question for Erlang dict

I read Joe Armstrong's "Programming Erlang" (Pragmatic Bookshelf). In the source code name_server.erl in chapter 16, where is the Dict variable ? Calling dict: new () automatically generates a dict? And, it is referenced that dict: new () creates a dictionary. Is it necessary to store it as a variable of type Dict = dict: new () ?

-module(name_server).
-export([init/0, add/2, whereis/1, handle/2]).
-import(server1, [rpc/2]).

add(Name, Place)  ->
  rpc(name_server, {add, Name, Place}).

whereis(Name) ->
  rpc(name_server, {whereis, Name}).

init()  ->
  dict:new().

handle({add, Name, Place}, Dict)  ->
  {ok, dict:store(Name, Place, Dict)};
handle({whereis, Name}, Dict) ->
  {dict:find(Name, Dict), Dict}.
+3
source share
1 answer

This is part of a two-file example. Another file (immediately before it in the book) server.erl. It contains a function loopthat calls the function handlein name_server.erl(or any other module that you pass to it):

Line:

{Response, State1} = Mod:handle(Request, State),

Mod - , start. State Mod:init() .

State name_server:init(), dict:new(). , loop , State State1.

, handle, Dict State.

+4

Source: https://habr.com/ru/post/1765009/


All Articles