简体   繁体   English

Elixir:为什么会出现“未定义函数”错误?

[英]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.

相关问题 为什么在Linux而不是Windows上出现错误“没有匹配的函数来调用A :: A(A)”的错误 - Why do I get the error “no matching function for call to A::A(A)” on Linux but not on Windows 为什么我用这段代码得到了一个错误的错误? - Why do I get an -fpermissive error with this code? 为什么我得到错误“NullReferenceException未处理”? - Why do I get the error “NullReferenceException was unhandled”? 为什么我在Java中遇到这个编译错误? - Why do I get this compile error in Java? 当我的代码在 function scope 之外时,为什么会出现编译器错误“未命名类型”? - Why do I get the compiler error “does not name a type” when my code is outside of a function scope? 为什么会出现此错误和警告? 错误和警告在描述中 - Why do I get this error and warning? Error and warning is in the description 为什么添加其他构造函数时会出现此错误? - Why do I get this error when I add another constructor? 为什么我会收到预期错误';' 在 ')' 标记之前? - Why do I get error expected ';' before ')' token? 为什么我得到 ':' expected.ts(1005) 作为 Typescript 错误? - Why do I get ':' expected.ts(1005) as a Typescript error? 为什么在我的代码生成程序中出现此语法错误? - Why do I get this syntax error in my code generating program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM