Proposal: Support of ETS guard conditions for Registry.match/3

75 views
Skip to first unread message

Yurko Hoshko

unread,
Feb 24, 2017, 2:41:28 AM2/24/17
to elixir-lang-core
Hello everyone.

There is a small proposal for improvement in Registry.match/3 .
At the moment it does not support ETS guard conditions, which could be quite useful.

Current behavior:
  @spec match(registry, key, match_pattern :: atom() | tuple()) :: [{pid, term}]
 
def match(registry, key, pattern) when is_atom(registry) do
    spec
= [{{key, {:_, pattern}}, [], [{:element, 2, :"$_"}]}]

   
...
 
end



Second element of spec list is a list of guards which is empty and non-changeable at the moment.
What we could do instead is allow passing additional guards for more precise lookup.

So it would be something like:

  @spec match(registry, key, match_pattern :: atom() | tuple(), guards :: list()) :: [{pid, term}]
 
def match(registry, key, pattern, guards \\ []) when is_atom(registry) and is_list(guards) do
    spec
= [{{key, {:_, pattern}}, guards, [{:element, 2, :"$_"}]}]

   
...
 
end

Example of guard list for checking if pattern variable is greater then 1: 
[{:>, :"$1", 1}]

It is a bit ugly syntax though - so it would be nice to hear if someone has better ideas to do, as well as is this feature useful at all.

Thanks :)

P.S. Link to Registry.match/3 on GitHub: GitHub

christ...@gmail.com

unread,
Feb 24, 2017, 4:39:42 AM2/24/17
to elixir-lang-core
I actually came to the forum to start a discussion on match specs in Elixir before noticing your post well before the my daily digest was issued, if you're interested.

José Valim

unread,
Feb 24, 2017, 5:14:10 AM2/24/17
to elixir-l...@googlegroups.com
Hi Yurko, please send a pull request that adds support for guards. :)

In the documentation we should make it clear that only the matchspec variables $1, $2 and friends must be used. If you use $$ or $_ there is no guarantee the representation will be kept.



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/ef366cd6-bb81-434b-8249-5948b0e735eb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yurko Hoshko

unread,
Feb 24, 2017, 5:21:39 AM2/24/17
to elixir-lang-core, jose....@plataformatec.com.br
Will prepare PR with functionality and documentation updates and post it as WIP once it is ready.

пʼятниця, 24 лютого 2017 р. 12:14:10 UTC+2 користувач José Valim написав:
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages