Can one create multiple module in a single elixir file and can we import functions from other elixir modules

General Question

Can a single elixir file contain multiple modules? and can one import functions from different modules?

1 Like

Yes, both is possible.

1 Like

Coool…and how do i import a function from a different module stored in a different .ex file?

1 Like

Just use the import special form.

2 Likes

As I’m now on a proper computer, and do not have to type on a mobile, I want to say, that I usually do not like to import. They usually pollute my own namespace with functions I might not even use.

Also when you find an unqualified call, you usually do look in the current module first and will probably not find it, but then you have to wade through a possibly long list of imports to wade through, to find the module from which the function is imported.

You should prefer a fully qualified remote call (e.g. MyApp.Repo.get(…)) or using aliases (e.g. alias MyApp.Repo; Repo.get or alias MyApp.Repo, as: R; R.get(…))

4 Likes