ETS and Mnesia

Apart from (emp-record ...) macros for ETS Match Patterns, which are also valid in Mnesia, LFE also supports match specifications and Query List Comprehensions. The syntax for a match specification is the same as for match-lambdas:

(match-spec
  ((arg ... ) {{(when e ...)}} ...) ;; Matches clauses
  ... )

For example:

(ets:select db (match-spec
                 (((tuple _ a b)) (when (> a 3))
                   (tuple 'ok b))))

It is a macro which creates the match specification structure which is used in ets:select and mnesia:select. The same match-spec macro can also be used with the dbg module. The same restrictions as to what can be done apply as for vanilla match specifications:

  • There is only a limited number of BIFs which are allowed
  • There are some special functions only for use with dbg
  • For ets/mnesia it takes a single parameter which must a tuple or a variable
  • For dbg it takes a single parameter which must a list or a variable

NOTE: The current macro neither knows nor cares whether it is being used in ets/mnesia or in dbg. It is up to the user to get this right.

Macros, especially record macros, can freely be used inside match specs.

CAVEAT: Some things which are known not to work in the current version are andalso, orelse and record updates.