I get a pid out of registry, the return can be a pid and undefined, how to pattern matching the pid?
case :syn.whereis("myProcess") do
++PID<x> -> do_something(x) # #PID<>
:undefined -> do_error_handle()
end
if I just use pid -> , will this match any situation including :undefined ?
pid when is_pid(pid) -> …
6 Likes
thank you @LostKobrakai , your solution works.
the code I tried first is:
case :syn.whereis("myProcess") do
pid -> do_something(pid) # #PID<>
:undefined -> do_error_handle()
end
Your problem was that Elixir/Erlang pattern matching tries from top to bottom to match, and if one matches then it stops. So as in your case pid
matches everything it just stopped there, if you would swap the lines then it will work as expected without any guards.
I already did that before I got the hint from @LostKobrakai, but I still want to know how to do that properly, so this the question created.
thank you @hauleth anyway.