Skip to content

Comment on Why Not Erlang? The Lack of Onrampsparent

Comments

The first hurdle is that Erlang works on modules by default. Those are expected to be compiled from a file. Now Elixir fixed that problem. For Erlang, it is probably doable via a compile-reload cycle hack. I guess it just hasn't been a priority. Maybe it should be...

Modules don't need to be compiled from files; there's just no batteries-included "eval these lines into a module" by default. But it's not hard:

    parse_form(Form) when is_list(Form) ->
      {ok, Tokens, _} = erl_scan:string(Form),
      {ok, AST} = erl_parse:parse_form(Tokens),
      AST.

    load_module(FormStrs) ->
      FormASTs = lists:map(fun parse_form/1, FormStrs),
      {ok, ModName, ModBin} = compile:forms(FormASTs),
      {module, ModName} = code:load_binary(ModName, "nofile", ModBin),
      {ok, ModName}.
Note that even an "anonymous" module must have a name, to serve as a handle for the VM. But you can do something like:
    gensym() ->
      {MT, T, UT} = erlang:now(),
      io_lib:format("~p_~p.~p.~p", [node(), MT, T, UT]).

    load_anonymous_module(FormStrs) ->
      ModNameFormStr = lists:flatten("-module(", gensym(), ")."),
      load_module([ModNameFormStr | FormStrs]).
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.