Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run erlang without compilation?

Tags:

erlang

runtime

Is there any VM for Erlang that allows you to do compilation on the fly instead of compiling before?

There is a possibility to compile from the shell, thanks Martin.

Now, from the Erlang shell (or some other module!):


1> compile:file("mymod.erl").
{ok,mymod}
2> mymod:myfun().
Hello Joe

Is there any pros or cons with doing this? Will you still be able to hot swap code? Is it the regular use-case to handle code? What benefits does the compiler give you in the end then?

like image 446
ptomasroos Avatar asked Sep 01 '25 05:09

ptomasroos


1 Answers

From the Erlang shell, you can compile a module on the fly using c("path/to/module.erl"). You can also access this functionality through the compile module, specifically the compile:file/{1,2} functions.

For example, suppose we have a file mymod.erl:

-module(mymod).
-export([myfun/0]).

myfun() -> io:format("Hello Joe~n").

Now, from the Erlang shell (or some other module!):

1> compile:file("mymod.erl").
{ok,mymod}
2> mymod:myfun().
Hello Joe

See Erldocs on the compile module for more information.

You can do a great deal with the Erlang compiler in runtime. For example, you can dynamically generate code for a module (use erl_syntax!) and then compile it without even writing it to a file using compile:forms/{1,2}.

(Insert standard speech on great power and great responsibility.)


Will you still be able to hot swap code?

Yes.

Is it the regular use-case to handle code?

No. Normally Erlang code is compiled ahead of time into BEAM bytecode. Depending on whether Erlang was started in embedded or interactive mode, the modules are either loaded on startup, or dynamically as they are referenced. If you are building a release, you basically have to compile ahead of time.

What benefits does the compiler give you in the end then?

Well, for one thing, we can build compact releases without unnecessary components like the compiler. Of course, we also get all the traditional benefits of ahead-of-time compilation, particularly that of not having to waste time compiling all the time.

To sum it up, unless you fully understand the implications and have a very good reason not to compile your code ahead of time, please follow the standard practices.

like image 148
Martin Törnwall Avatar answered Sep 03 '25 16:09

Martin Törnwall