erlang:用“fun”调用的函数应该用parse_transform进行转换? [英] erlang: function called with real 'fun' should be transformed with parse_transform?

查看:1191
本文介绍了erlang:用“fun”调用的函数应该用parse_transform进行转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

我正在看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 函数。



官方文档


fun2ms(LiteralFun) - > MatchSpec



Pseudo 功能,通过parse_transform将
LiteralFun键入为函数中的参数调用一个match_spec。
literal意味着乐趣需要以文本方式写成
函数的参数,它不能保存在一个变量中,而
又被传递给功能
)。



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:

fun2ms(LiteralFun) -> MatchSpec

Pseudo function that by means of a parse_transform translates LiteralFun typed as parameter in the function call to a match_spec. With "literal" is meant that the fun needs to textually be written as the parameter of the function, it cannot be held in a variable which in turn is passed to the function).

这篇关于erlang:用“fun”调用的函数应该用parse_transform进行转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆