Why is Process.registered?/1 not a function?

This seems like a no-brainer function that could be really handy, but it doesn’t exist. In my umbrella application, I use send/2 to send messages between my applications. In my tests I use assert_receive to verify that my messages are sent. When I run the tests at the application level, I need to register the named process of the other application with self() to use this assertion. But this doesn’t work at the umbrella-level, because I first need to unregister the named process, then register it with self(). If I had a function, Process.registered?/1, this would be super easy.

I’m wondering whether this is an edge case or I should submit a PR to the standard library for this function:

@spec registered?(atom) :: boolean
def registered?(name) do
  names = Process.registered()
  Enum.any?(names, fn n -> n == name end)
end
2 Likes

Since Process.registered() returns a list you can probably just do:

@spec registered?(atom) :: boolean
def registered?(name) do
  name in Process.registered()
end
3 Likes
  def registered?(name),
    do: Process.whereis(name) != nil

Process.whereis/1

6 Likes

Even easier!

1 Like