Registry.select guard - that checks if a value is in a list using the in/2 operator?

Hello all.

I am trying to find a process via function Registry.select/2. Guards seem to work for comparison operators like :>. But I would like to implement a guard that checks if a value is in a list using the in/2 operator.

The documentation for the guards states that it should be possible:
https://hexdocs.pm/elixir/guards.html

The following code has two guards. The first one works, but the second one fails with an error: * 2nd argument: not a valid match specification

Registry.select(Registry, [
        {
          # match_pattern
          {
            # key
            {:id, :"$1"},
            # pid
            :"$2",
            # value - username is a string, roles is a list of strings
            %{username: :"$3", roles: :"$4"}
          },
          # guards
          [
            # this guard works
            {:=, :"$3", "test_user"},
            # this guard fails with an error: * 2nd argument: not a valid match specification
            {:in, :"$4", ["admin", "support"]}
          ],
          # body
          [%{username: :"$3", roles: :"$4"]
        }
      ])

Is the “in” operator supported in the Registry.select function. If so, how do I use it?

1 Like

in is an elixir specific API, which at compiletime is changed to many equals comparisons, joined by or: Kernel — Elixir v1.14.1

Matchspecs come from OTP and therefore don’t know about this guard, nor implement it. You can however use the same translation elixir does for your matchspec.

4 Likes