简体   繁体   English

Elixir 未定义函数

[英]Elixir Undefined function

Elixir beginner here. Elixir 初学者在这里。 I am attempting to run a hello world elixir script from the iex我正在尝试从 iex 运行一个 hello world elixir 脚本

The script is a basic hello world example该脚本是一个基本的 hello world 示例

IO.puts "Hello World!"

I run the following command from iex我从 iex 运行以下命令

iex(1)> elixir hello.exs

and get this error:并收到此错误:

** (CompileError) iex:1: undefined function elixir/1

Not sure why I am getting an error, any help would be appreciated.不知道为什么我收到错误,任何帮助将不胜感激。 Thanks谢谢

Just so the answer is not buried in a comment:只是这样答案就不会埋在评论中:

You should run elixir hello.exs from the shell, not inside iex您应该从 shell 运行elixir hello.exs ,而不是在iex

assuming you are in your console.假设您在控制台中。

$ elixir hello.exs

see: Running scripts for elixir请参阅: 运行长生不老药的脚本

As another answer rightly suggest, there is no "elixir" function in the interactive elixir shell (iex), so what you meant to do was to execute the elixir command from your system shell.正如另一个正确的答案所暗示的那样,交互式 elixir shell (iex) 中没有“elixir”功能,因此您要做的是从系统 shell 执行 elixir 命令。

But, there are helpers in iex that can load files from the directory iex was started in—first a bit of setup, given we have a test.exs file in our current working directory with the following contents:但是,iex 中有一些帮助程序可以从 iex 启动的目录加载文件——首先是一些设置,假设我们在当前工作目录中有一个test.exs文件,其中包含以下内容:

defmodule Test do
  def greet(person) do
    "Hi, #{person}!"
  end
end

We could load that into our iex session by using the c/1 (compile file) helper from iex:我们可以使用来自 iex 的c/1 (编译文件)助手将其加载到我们的 iex 会话中:

$ iex
Erlang/OTP 22 [erts-10.5.5] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [hipe]

Interactive Elixir (1.9.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> c("test.exs")
[Test]
iex(2)> Test.greet("Pau")
"Hi, Pau!"
iex(3)>

Other helpers in iex that might help you here is pwd that will print the working directory, and ls that will list the files in the current working directory—you can change the working directory by using the cd helper, that takes a directory as a sting as an argument. iex 中可能对您有所帮助的其他帮助程序是pwd将打印工作目录,而ls将列出当前工作目录中的文件 - 您可以使用cd帮助程序更改工作目录,它将目录作为字符串作为论据。

I'm also new to Elixir and couldn't figure out why I was receiving "undefined function error" when trying to invoke my "create_deck" example function after having typed iex -S mix from the cards directory.我也是 Elixir 的新手,无法弄清楚为什么我在从cards目录键入iex -S mix后尝试调用我的“create_deck”示例函数时收到“未定义的函数错误”。 Turns out, I was not including the module name when trying to invoke from the interactive shell.事实证明,我在尝试从交互式 shell 调用时没有包含模块名称。 I was supposed to type Cards.create_deck() , rather than just create_deck .我应该输入Cards.create_deck() ,而不仅仅是create_deck

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

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