繁体   English   中英

定义函数,如果没有答案,则回叫:Elixir

[英]Defining function then calling back if no answer: Elixir

如果没有响应,请尝试回调该函数。 这更多是面向对象的吗?

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)

模块名称必须大写。

只需替换第一行

defmodule ask do

defmodule Ask do

关于递归调用函数:这与对象定向完全没有关系,在许多情况下,这种用法在Elixir中很常见。 但是,您可以像在同一个模块中一样,只调用getAlt()而不用模块名( Alt.getAlt() )作为前缀。

附带说明:Elixir中通常不使用骆驼式保护套。 通常应该将您的函数称为get_alt()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM