Handling multiple Elixir files?

One of the earliest programming languages I’ve used was C. In C, you could simply compile two programs (say, foo.c and bar.c) and use header files, so that one file (foo.c, say) could access functions in another file. So I’m used to coding in this manner without resorting to some kind of build tool (mix for Elixir or lein for Clojure).

I’ve been able to run code using a single Elixir file. I typically type “elixir foo.ex” to run the code, and I like the simplicity of that.

I know mix is probably the way to go, but is there something like being able to use foo.ex and bar.ex without using mix to set it up which would allow me, say, to run the code in foo.ex, but refer to code in bar.ex. Or is that so non-standard, that I should be using mix?

And if so, any recommendations on which mix tutorial to look at?

However, in C you still had to be sure to compile things in the right order and link them in the right order, that is the purpose of a build system, like CMake in the C/C++ world. :slight_smile:

I’ve been able to run code using a single Elixir file. I typically type “elixir foo.ex” to run the code, and I like the simplicity of that.

Really meant for exs instead of ex files. exs states it is a ‘script’ (with top level code and all) and ex designates it is a module(s). Although the extension is mostly documentative when passed directly in though. ^.^;

elixirc is what actually compiles the ex to beam files, so you can do that to link files in, then specify the path to them (defaults to the current directory), here is an example shell session without mix:

╭─my_user@my_machine ~/tmp/tmp  
╰─➤  ls
bar.ex  foo.ex
╭─my_user@my_machine ~/tmp/tmp  
╰─➤  cat bar.ex
IO.puts("Foo's blah is:  #{Foo.blah()}")

╭─my_user@my_machine ~/tmp/tmp  
╰─➤  cat foo.ex
defmodule Foo do
  def blah, do: 42
end

╭─my_user@my_machine ~/tmp/tmp  
╰─➤  elixirc foo.ex
╭─my_user@my_machine ~/tmp/tmp  
╰─➤  ls
bar.ex  Elixir.Foo.beam  foo.ex
╭─my_user@my_machine ~/tmp/tmp  
╰─➤  elixir bar.ex
Foo's blah is:  42

But really, handling the compiling in the right order and all that mess is painful, just use mix. ^.^

The standard elixir tutorials on mix is fine, but really just start by running mix new and reading when it tells you. Then put source code in the lib directory in your new projects directory. :slight_smile:

mix help for lots of help too, can run mix help <cmd> for any specific mix command too.

3 Likes

That was quick! So, basically, use mix if you want to do more than one file (and probably one file) and put the files in the lib folder. Gotcha. Guess I need to dive into mix at some point, and now is pretty good.

Mix is dead-simple, one of the easiest build systems out (certainly easier than anything in the C world for sure). ^.^

But yep yep. :slight_smile:

That was quick! So, basically, use mix if you want to do more than one file
(and probably one file) and put the files in the lib folder. Gotcha. Guess I
need to dive into mix at some point, and now is pretty good.

“mix new” also automatically sets up a test harness for you, ready
to run out of the box.

I love writing tests. :grinning:

1 Like

How do you decide what tests to write?

Same as in any other language. It’s good to write tests to test every callpath through your public functions. :slight_smile:

How do you decide what tests to write?

Write the tests that describe the feature/functions that you want and
are about to write :grinning:

IMO writing a test and making it pass in an idiomatic way is a great
learning mechanism, aside from the benefits of having well-tested
production code.

1 Like