【问题标题】:Can match specifications be used only for tracing and selecting from ets tables? Can they be used in a case expression?匹配规范只能用于跟踪和从 ets 表中选择吗?可以在 case 表达式中使用它们吗?
【发布时间】:2012-03-07 04:45:07
【问题描述】:

有没有办法使用匹配规范在函数的不同子句之间进行选择?我见过匹配规范仅用于跟踪或匹配 ets 表中的条目。

我想做的例子:

在用户提供的文件中:

Module(m1),  
Function(f1),  
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] }  
  ]).

我想从这个文件生成代码。对我来说重要的是能够使用 Guard 中的匹配规范来过滤掉 f1 的子句,这样我就可以知道 f1 的第一个参数何时是 {score, First, Second,第三}和is_atom(First)、is_pid(Second)、is_atom(Third)。

有没有办法让我生成这样的代码:

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

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: erlang


    【解决方案1】:

    看看ets:match_spec_compile/1 和ets:match_spec_run/2,它们完全符合您的要求。

    【讨论】:

    • 谢谢亚当。我暂时退出了比赛规格,并采取了不同的方法。但是我不知道这些功能,所以再次感谢!
    猜你喜欢
    • 2014-09-03
    • 2015-01-16
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-04
    相关资源
    最近更新 更多