在 Elixir 的捕获运算符中忽略匿名函数的参数

Ignore arguments for anonymous functions in capture operator in Elixir

我想创建一个匿名函数,例如:fn _x -> "random" end 在 Elixir 的捕获中,但是如果我这样做的话:

&("random") 然后我得到一个编译错误:

** (CompileError) invalid args for &, expected an expression in the format of &Mod.fun/arity, &local/arity or a capture containing at least one argument as &1, got: "random"

有办法实现吗?谢谢

正如the documentation

中明确指出的

The only restrictions when creating anonymous functions is that at least one placeholder must be present, i.e. it must contain at least &1, and that block expressions are not supported.

也就是说,&("random") 符号是不可能的,应该写成 fn _ -> "random" end