chrisdel101

chrisdel101

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?)

Showing Posts 1 to 10

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

hauleth

hauleth

Also, both of your functions aren’t exported, so you cannot call them outside of the module.

chrisdel101

chrisdel101 OP

If I run the migrarions ecto.migrate I immediately get

(CompileError) priv/repo/migrations/20230227174711_MyModule.exs:26: undefined function sum2/2 (there is no such import).

Same for sum1

That is with this line inside the module IO.print(sum2(1,2))

Shouldn’t this solve that issue?

chrisdel101

chrisdel101 OP

Don’t know to export functions. Looking that up. Link to examples possible? :slight_smile:

Also, see the case where the function is called inside the module. I get the same error for both cases.

hauleth

hauleth

In Elixir? Just use def instead of defp.

chrisdel101

chrisdel101 OP

Sorry I had a typo. One is public, one is private - only for example here. I don’t have it as private in practice.

Makes no difference here. Get same error

I see what U mean, that that is required to export. But I’m trying to call within the same module. I’m not trying to do anything at all fancy - just write a function and call it.

gregvaughn

gregvaughn

This may really be an XY Problem. Are you familiar with mix run? It lets you run an Elixir function from the command line.

chrisdel101

chrisdel101 OP

If I call mix run /priv/repo/migrations/my_module.exs I get undefined function sum/2 (there is no such import)

I’m not sure I’ll be able to call the file like this anyway, since it’s a phoenix file? Still, error is still the same error.

I’ll try moving the function out to another file. If u see the replit link I added this is not specific to phoenix or migrations (just calling a function inside a module - the real question), and it still causes the error.

It might make U login to this though, not sure.

chrisdel101

chrisdel101 OP

Deleted reply

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews