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

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement