Can spec be used only for tracking and selection from ets tables? Can they be used in a case statement?

Is there a way to use a conformance specification to choose between different feature clauses? I've seen match specs only used to track or match records in ets tables.

An example of what I want to do:

In the file provided by the user:

Guard([ %% list of match specifications follows:  
  %% First (and only in this case) match spec:  
   { [{score, '$1', '$2', '$3'}, '$4'], 
       [{is_atom, '$1'}, {is_pid, '$2'}, {is_atom, '$3'}], 
     [true] }  


From this file I want to generate code. It is important for me to be able to use the match specs in Guard to be able to filter out the f1 clauses so that I can know when the first argument to f1 was a tuple of the form {score, First, Second, Third} and is_atom (First), is_pid (Second ), is_atom (Third).

Is there a way to generate the code like this:

case some_unknown_function(MatchSpec, F1Args) of
  true ->
    %% f1 clause matches the MatchSpec
  false ->
    %% f1 clause does not match the MatchSpec


Any help would be much appreciated. Thank.


source to share

1 answer

Take a look at ets:match_spec_compile/1

and ets:match_spec_run/2

, they do exactly what you want.



All Articles