问题描述
我正在看O'Reilly Erlang编程书,有一个例子是在erlang shell中运行,如下所示: code> 17>当Job / = cook - >
[Country,Name] end时,MS = ets:fun2ms(fun({Name,Country,Job}))。
[....一个erlang匹配表达式被返回....]
18> ets:select(countries,MS)。
[[爱尔兰,sean],[爱尔兰,克里斯]]
然而,当我在我的代码(不在shell中)做类似的事情:
Fun = fun({Type,_,_,ObjectId, PlayerId})当Type == player_atom,PlayerId == 2 - > ObjectId end,
MatchFun = ets:fun2ms(Fun),
PlayerObjectId = ets:select(?roster_table,MatchFun),
我得到FUBAR:
exit:{badarg,{ets,fun2ms,功能,调用,与,真实,'乐趣',应该,转换,与,parse_transform,'或',调用,一个,'乐趣',生成,在,shell]}}
(除此之外,我想知道为什么错误不是....用....调用的函数可能这样io :格式(〜p,TheErrorMessage)将换行?)
无论如何,我已经放弃选择有利于ets:foldl,因为后者工作和 - 通过有趣的例外 - 允许我在找到第一个项目时终止遍历。但是,我仍然好奇...
... (我在parse_transform上做了一些阅读,我已经足够新鲜了,我错过了连接。)
badarg
异常是使用错误的参数调用的内置函数(或在这种情况下的伪函数)的症状。在这种情况下, ets:fun2ms / 1
函数。
从:
I'm looking at the O'Reilly Erlang Programming book and there's an example that is run in the erlang shell that looks like this:
17> MS = ets:fun2ms(fun({Name,Country,Job}) when Job /= cook ->
[Country,Name] end).
[ ....an erlang match expression is returned.... ]
18> ets:select(countries, MS).
[[ireland,sean],[ireland,chris]]
However, when I do something similar in my code (not in the shell):
Fun = fun({Type,_,_,ObjectId,PlayerId}) when Type==player_atom, PlayerId==2 -> ObjectId end,
MatchFun = ets:fun2ms(Fun),
PlayerObjectId = ets:select(?roster_table, MatchFun),
I get FUBAR:
exit:{badarg,{ets,fun2ms,[function,called,with,real,'fun',should,be,transformed,with,parse_transform,'or',called,with,a,'fun',generated,in,the,shell]}}
(As an aside, I wonder why the error isn't 'function called with....' Probably so io:format("~p", TheErrorMessage) will line wrap?)
Anyway, I have abandoned select in favor of ets:foldl, since the latter works and - through exceptions in the fun - allows me to terminate the traversal when the first item is found. But, I'm still curious...
...wha? (I did some reading on parse_transform, and I'm new enough to erlang that I'm missing the connection.)
The badarg
exception is symptom of a built-in function (or a pseudo function, as in this case) called with a wrong parameter. In this case, the ets:fun2ms/1
function.
Reading from the official documentation:
这篇关于erlang:用“fun”调用的函数应该用parse_transform进行转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!