[英]Elixir: Why do I get “undefined function” error?
The Elixir compiler gives me "undefined function changes/0" in the third line of the function: Elixir编译器在函数的第三行给了我“未定义的函数更改/ 0”:
def ignore_empty(changeset) do
cond do
%{valid?: false, changes: changes} = changeset when changes == %{} ->
%{changeset | action: :ignore}
true -> changeset
end
end
Why is that? 这是为什么? I mostly copied this code from Ecto.Changeset documentation of the cast_assoc function here
我大多复制从cast_assoc功能Ecto.Changeset文档这段代码在这里
You probably meant to use case
, eg 您可能打算用
case
,例如
def ignore_empty(changeset) do
case changeset do
%{valid?: false, changes: changes} = changeset when changes == %{} ->
%{changeset | action: :ignore}
_ ->
changeset
end
end
The reason you are getting "undefined function" is because when the expression is getting expanded, that happens to be the first error the compiler encounters. 之所以得到“未定义的函数”,是因为在扩展表达式时,恰巧是编译器遇到的第一个错误。 It's valid to put an expression in a
cond
, so other than the reference to an undefined function changes
the other thing which it would fail on is the use of when
. 将表达式放入
cond
是有效的,因此,除了对未定义函数的引用changes
以外,它会失败的另一件事是使用when
。 In any case, that appears to be the issue here. 无论如何,这似乎是这里的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.