0
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.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
Raaz444
  • 85
  • 1
  • 7
  • I don't see any four argument version of `gen_server:call` in the documentation: http://erlang.org/doc/man/gen_server.html#call-3. – Dogbert Jun 29 '18 at 18:19

1 Answers1

3

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.

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
  • What does [], [] these things do at the end of the second line, i removed one of those square brackets to match the gen_server:call/3 the error has been fixed. gen_server:call({local, UserDefined}, terminate, []), – Raaz444 Jul 01 '18 at 10:11
  • Those are just empty lists. Removing one of them will fix the arity of the function call, but it still won't work, because an empty list is not a valid timeout value. – Roger Lipscombe Jul 01 '18 at 17:49