是否可以?如果是这样,如何?
以下代码在 IEX 中执行。
但是,编译后的代码会生成运行时错误。
:ets.fun2ms(fn({a,b}) -> a and b end)
错误是这样出现的:我想知道如何正确调用。
** (exit) exited in: :ets.fun2ms(:function, :called, :with, :real, :fun, :should, :be, :transformed, :with, :parse_transform, :or, :called, :with, :a, :fun, :generated, :in, :the, :shell)
** (EXIT) :badarg
stacktrace:
(stdlib) ets.erl:554: :ets.fun2ms/1
test/game/ets_lookup_test.exs:27
最佳答案
不,你不能。至少不是像错误所说的“真正的功能”。 Elixir 函数的定义与 Erlang 中的函数略有不同,这就是该函数不起作用的原因。幸运的是,您可以使用此存储库完成相同的操作 https://github.com/ericmj/ex2ms
正如自述文件中所述:
iex(1)> import Ex2ms
iex(2)> fun do { x, y } = z when x > 10 -> z end
[{{:"$1",:"$2"},[{:>,:"$1",10}],[:"$_"]}]
iex(3)> :ets.test_ms({ 42, 43 }, v(2))
{:ok,{42,43}}
iex(4)> :ets.test_ms({ 0, 10 }, v(2))
{:ok,false}
宏
Ex2ms.fun/1
与 ets:fun2ms/1
相同.我希望这有帮助。
关于elixir - 如何在 elixir 中调用 :ets. fun2ms?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37968208/