【问题标题】:Defining function then calling back if no answer: Elixir定义函数,如果没有答案则回调:Elixir
【发布时间】:2019-02-08 10:54:05
【问题描述】:

如果没有响应,则尝试回调函数。这更像是面向对象的东西吗?

defmodule ask do
 def getAlt(altitudeSet) do

  altitudeSet = IO.gets"altitudeSet"

  case altitudeSet do
    altitudeSet when altitudeSet in ["Y\n","y\n"]->
      IO.puts "You selcted #{altitudeSet}"
    altitudeSet when altitudeSet in ["N\n","n\n"]->
      IO.puts "You selected #{altitudeSet}"
    _->
     ask.getAlt()
   end

 end
end

我明白了:

warning: variable "ask" does not exist and is being expanded to "ask()", please use parentheses to remove the ambiguity or change the variable name newGlennHopperScript.exs:9 ** (CompileError) newGlennHopperScript.exs:9: undefined function ask/0
(stdlib) lists.erl:1354: :lists.mapfoldl/3
(elixir) expanding macro: Kernel.defmodule/2
newGlennHopperScript.exs:9: (file)

【问题讨论】:

  • 使用 Ask 而不是 ask - 在 Elixir 中模块必须以大写字母开头

标签: function elixir


【解决方案1】:

模块名称必须大写。

只需替换第一行

defmodule ask do

defmodule Ask do

关于递归回调函数:这与面向对象完全没有必然联系,在 Elixir 中这种用法很常见的情况很多。但是,您可以直接调用 getAlt() 而不是在其前面加上模块名称 (Alt.getAlt()),因为您在同一个模块中这样做。

附带说明:驼峰式在 Elixir 中并不常用。你的函数通常应该被称为get_alt()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 2020-08-25
    • 1970-01-01
    • 2021-04-03
    • 2011-08-11
    • 1970-01-01
    • 2019-09-14
    相关资源
    最近更新 更多