Hacker News new | ask | show | jobs
by resc1440 3902 days ago
Are macros useful in a web development context? I don't feel like I've been missing them in Django, but maybe I've just forgotten because it's been a long time since I wrote anything substantial in Scheme. (Are Elixir macros similar to Scheme macros?)
1 comments

Macros are the most powerful tool for making DSLs, which means they pop up in a lot of "specialized" niches, like web dev. You can make DSLs in other ways, but without the ability to inspect and decompose code at the per-token level, there are limitations to what can be done.

Elixir macros are like Scheme macros; they're fully hygienic (no variable namespace issues), and they evaluate at compile time. The main difference is that Scheme's main syntax construction is the linked list, and Elixir uses a richer data structure, almost equivalent to JSON. Aside from the particular structure the macro's manipulating, the concepts are the same.

Elixir macros are also used idiomatically to allow "smart" loading of modules, e.g. specifying additional code to run in the caller's context rather than the callee's. They fit nicely for this purpose.

Elixir is great, and I would loosely say it not homoiconic as Robert Virding, one of the original Erlang team and creator of LFE (Lisp Flavored Erlang), posits in an exchange with Jose Valim, creator of Elixir here[1]. In terms of qualifying a JSON-equivalent data structure as richer, compared to lists in Scheme or Lisp, I would disagree. I think you lose the code-as-data (down to the languages fundamentals) win in Scheme. Devin Torres makes the statement that Elixir is 'strongly homoiconic' [2], but his reference's definition does not fit Elixir [3]. You cannot add new constructs or operators in Elixir. I would say 'weakly homoiconic' if you are going to prefix an adverb on a page that invents terms, which I still think is like 'a little bit pregnant' - not logically consistent. I do not code for a living, so I can choose things on an interest basis, not a popularity or job market index. I am learning both Elixir and LFE, and Erlang by default, but I am gravitating towards LFE. I think LFE on the BEAM and Clojure on the JVM, makes for a good argument for me to stay in the Lisp world, and not the Ruby, ROR, or Elixir one.

[1] https://groups.google.com/forum/#!topic/lisp-flavoured-erlan...

[2] http://devintorr.es/blog/2013/06/11/elixir-its-not-about-syn...

[3] http://c2.com/cgi/wiki?HomoiconicLanguages

Thanks for the reply! I read the "[1]" link you posted, and the word "homoiconic" doesn't appear anywhere. Elixir code and data share the same native data structures, ergo Elixir is homoiconic. Virding's point is that Elixir lacks reader macros. For real-world usage, I consider that more of a strength than a weakness. :)
Thanks for this interesting answer! I read most of the getting started guide last night and I'll probably try Elixir for a toy project sometime soon :)