charlzz

charlzz

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?

Showing Posts 1 to 7

OvermindDL1

OvermindDL1

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.

charlzz

charlzz OP

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.

OvermindDL1

OvermindDL1

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:

hassan

hassan

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_face:

charlzz

charlzz OP

How do you decide what tests to write?

OvermindDL1

OvermindDL1

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

hassan

hassan

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_face:

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New

Other Trending Topics Top

KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews