Raaz444
Raaz444

Reputation: 85

Dialyzer Errors call to missing or unexported function gen_server:call/4

stop_link(UserDefined) ->
  gen_server:call({local, UserDefined}, terminate, [], []),
  ok

I am using dialyzer to fix warning in erlang code, I came across this mistake which reads missing or unexported function gen_server:call/4.

I am not able to understand what is wrong with this can, anyone please guide me in what the mistake is I had just started with Erlang I would greatly appreciate if you can explain it briefly.

Upvotes: 0

Views: 1066

Answers (1)

Roger Lipscombe
Roger Lipscombe

Reputation: 91965

There are many things wrong with this code. Here goes...

The reason the start_link function is called that is because it starts the process and links to it. Your stop function should just be called stop.

The documentation for gen_server:call/2,3 shows up two problems with this code:

  1. You don't need the {local, Name} form with gen_server:call. You only need it when calling gen_server:start_link (and only then if you want a registered name for your process). For calling local names, just use Name. Or the process ID.
  2. There isn't a variant of the function with arity 4 (i.e. 4 parameters). The 3-arity variant takes a timeout. You probably want the 2-arity one.

I suspect that you're trying to specify an arbitrary function in gen_server:call (i.e. you want to call the terminate function). That's not how this works.

gen_server:call(NameOrPid, Request) results in a call to handle_call(Request, From, State). See the documentation.

In that function, you can match the request and do the appropriate thing. Something like this:

handle_call(frob, _From, State) ->
    % do whatever 'frob' means.
    {reply, ok, NewState};

(that ; might be a ., depending on whether this is the final handle_call clause).

If you really want the server to stop, you should just do the following:

handle_call(terminate, _From, State) ->
    {stop, meh, State}.

That will result in a call to terminate.

Oh, and if you're only just learning Erlang, you probably don't want to be running dialyzer until you've got a bit more experience. It's a bit ... tricky ... for the uninitiated. Though it did find this mistake, which was nice.

Upvotes: 2

Related Questions