2

I am new to Elixir. I am trying to run a function inside a module. My code in the file is as follows:

defmodule greeter do

  def print() do
    IO.puts "Hello workd" 
  end

  def print(name) do
    IO.puts "Hello " <> name 
  end

  defp print(name,age) do
    IO.puts "Hello " <>name<>" My age is "<> age 
  end

end

greeter.print()
greeter.print("Xyxss")

When I run elixirc filename.ex on my command line I get the following error:

warning: variable "greeter" does not exist and is being expanded to "greeter()", please use parentheses to remove the ambiguity or change the variable name
functions.ex:1
== Compilation error in file functions.ex ==
** (CompileError) functions.ex:1: undefined function greeter/0
   (stdlib) lists.erl:1354: :lists.mapfoldl/3
   (elixir) expanding macro: Kernel.defmodule/2
   functions.ex:1: (file)

I am unable to solve the given error. Can somebody help me with this?

2 Answers 2

1

I would put a correct answer here, since the answer provided by @J.Sebio is plain wrong.

The module name in Elixir must be an atom. Both examples below work perfectly:

iex(1)> defmodule :foo, do: def yo, do: IO.puts "YO"
iex(2)> :foo.yo
YO

iex(3)> defmodule :"42", do: def yo, do: IO.puts "YO"  
iex(4)> :"42".yo                                     
YO

The thing is: in Elixir, capitalized term is an atom:

iex(5)> is_atom(Greeting)
true

That is why capitalizing the name of the module worked. Also, greeting is a plain variable, that is why the compiler tries to resolve it inplace and throws an error.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't know that I'd say his answer is "plain wrong"--more of an issue with him misunderstanding what the initial capital letter is doing in the code. He's sort of right but for the wrong reason.
@OnorioCatenacci I admit I exaggerated the wording, but “In elixir the modules are written capitalized” is sorta fraud. The main thing here is to understand the root cause in the first place, not to write everything starting with a capital letter. And btw modules exported from erlang are still lowercased :observer.start.
0

In elixir the modules are written capitalized, and normally written CamelCase, so, in your case you have to rewrite your code to:

defmodule Greeter do

    def print() do
        IO.puts "Hello workd"
    end

    def print(name) do
        IO.puts "Hello " <> name
    end

    defp print(name,age) do
        IO.puts "Hello " <>name<>" My age is "<> age
    end

end

Greeter.print()
Greeter.print("Xyxss")

5 Comments

I didn't know that. I changed the function to CamelCase and it worked. Thanks. It would be great if you could the link in the documentation of elixir where it is mentioned.
@shubhamagiwal92 you're welcome. I think you can't find it in explicit way, but here, you can read defmodule HelloModule, you can imagine that modules are capitalized.
This is wrong, please see my answer for the correct explanation.
Thanks @Onorio Catenacci. @mudasobwa I am not "plain wrong". I am sure that you can't find any official elixir's documentation where a module was defined like an atom, @shubhamagiwal92 asked here for a solution to his problem and my solution is valid, why not? The fact that a capitalized word in Elixir is a atom is a internal thing, when Elixir converts the module to a atom like :"Elixir.Greeter" in this case.
Is Module.concat/1 official enough? Also, blindly capitalize the name “just because everybody does” is not a solution, it’s a kludge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.