chrisdel101

chrisdel101

Calling function from within same module it's declared is undefined/is not available

Redoing this to be more clear:

Question

How can I call a function inside a module?

File: test.exs

defmodule MyModule do

  def sum(a, b) do
    a + b
  end

  MyModule.sum(1,1) # or sum(1,1)
end

elixir test.exs :

** (UndefinedFunctionError) function MyModule.sum2/2 is undefined (function not available)
    MyModule.sum2(1, 1)

==================================================
This section can be skipped
Original question asking same thing but more confused:
This is an .exs file used for Phoenix migrations, so it’s inside the folder /priv/repo/migrations. (In case that matters). I wrote a function but it’s totally blocked. I don’t want to import it from another module since this is where it goes (declared and used in same module)


defmodule MyApp.Repo.Migrations.MyModule do
  use Ecto.Migration

  def sum1(a, b) do #public
    a + b
  end
 defp sum2(a, b) do #private
    a + b
  end

end

From iex -S mix

iex(2)> MyApp.Repo.Migrations.MyModule
MyApp.Repo.Migrations.MyModule
iex(3)> MyApp.Repo.Migrations.MyModule.sum2
** (UndefinedFunctionError) function MyApp.Repo.Migrations.MyModule.sum2/0 is undefined (module MyApp.Repo.Migrations.MyModule is not available)
    MyApp.Repo.Migrations.MyModule.sum2()

iex(4)> MyApp.Repo.Migrations.MyModule.sum1(1,1)
** (UndefinedFunctionError) function MyApp.Repo.Migrations.MyModule.sum1/2 is undefined (module MyApp.Repo.Migrations.MyModule is not available)
    MyApp.Repo.Migrations.MyModule.sum1(1, 1)
    iex:4: (file)

If U can’t call it like this, this is what I want to do. It gives the same error as the above attempt.

defmodule MyApp.Repo.Migrations.MyModule do
  use Ecto.Migration

  def sum1(a, b) do
    a + b
  end
 defp sum2(a, b) do
    a + b
  end
IO.puts(sum1(1,1)) #or sum2(1,1)
end

Pls Ignore it being bad practice (if it is) to have a function in a migration file, unless this the cause.

Real use case (Not sure if this link is accessible to all?)

Most Liked

gregvaughn

gregvaughn

In iex you have not loaded the migration module. You’ve just defined the module name as an atom. Since it’s a .exs instead of .ex it is not compiled and loaded automatically with iex -S mix

dimitarvp

dimitarvp

Replace this with:

defmodule MyModule do
  def sum(a, b) do
    a + b
  end
end

MyModule.sum(1,1)

Namely call it outside of the module, not inside of it. When you are trying to run code inside the module body – but NOT inside a function body – then you are effectively trying to run code that wasn’t compiled yet (if I remember correctly).

So if your goal is to define a module with functions and then immediately call them, my above code is a good way to do it.

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement