Reputation: 85
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
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:
{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.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