3 ms·
Haven't touched Erlang but has an entry level knowledge on Elixir. I often heard how easy it is to write macros on Elixir. Can you elaborate on this?
by alskdj21 7y ago
Haven't touched Erlang but has an entry level knowledge on Elixir. I often heard how easy it is to write macros on Elixir. Can you elaborate on this?
- arjan_sch 7y agoOne of the big differences between Erlang and Elixir is indeed that Elixir allows you to write macros in the language itself, using quote / unquote constructs just like lisps. This makes it easy to generate code on compile time, which is then executed in runtime without any performance penalty. A large part of Elixir itself is actually implemented as macros. For instance, the "unless" construct: defmacro unless(condition, do: do_clause, else: else_clause) do quote do if(unquote(condition), do: unquote(else_clause), else: unquote(do_clause)) end end (code simplified for clarity)
- fcbrooklyn 7y agoAnd the if-else construct is also a macro. The whole thing ends up being a case I think.
- dnautics 7y agoImho You shouldn't really write macros, they are there for libraries to make it easy to do the right thing with less boilerplate.