A morning with elixir
Saturday, 1st June, 2013
After reading Joe Armstrong’s recent blog post on elixir, and the ensuing discussion there and on twitter, I’ve thought a bit about why I don’t like the language. I can’t spend a week with elixir, so the three observations below are after a morning’s reading and watching.
My overall impression is that elixir is a language that makes it easy for people with a ruby background to dabble with erlang. However, after a morning I’ve found at least three things about elixir that would make my own programming life less pleasant (see below). Elixir contains apparently powerful new features (e.g., macros and protocols), but the examples I’ve seen (e.g. in José Valim’s Erlang Factory talk) are not very exciting.
word-like forms as delimiters
# elixir  def name ( arglist ) do code end % erlang  name ( arglist ) -> code .
I fail to see how  is an improvement on .  is shorter by a handful of characters but, mainly,  uses word-like forms as delimiters, which I think is a very bad idea.
A human reader will read those word-like forms as words (e.g., the English words “do” and “end”), which they’re not. The delimiters “def”, “do”, and “end” delimit the function definition in the same way that “(” and “)” delimit the argument list.
Using word-like forms as delimiters will either/both (a) slow down the human reader as they read these forms as words, or/and (b) make the code harder to read, as the human reader must explicitly remember not to interpret certain word-like forms as words.
cf also lc and inlist or inbits used to delimit comprehensions.
n.b.: the same arguments apply to erlang’s use of “end” as a delimiter for fun and case blocks. Presumably erlang’s excuse was that they ran out of delimiters borrowed from English punctuation, and they didn’t want to overload “.”. I wonder whether some kind of list delimiter might be appropriate (for case at least), e.g.:
R = case f() of [this -> 1; that -> 2], g(R).
The pipeline operator |>
The pipeline operator strikes me as being halfway between useful and troublesome. It would probably be very useful in cases similar to the example Joe gives (repeated below for convenience) — where a series of functions each take and produce a single item of data.
capitalize_atom(X) -> V1 = atom_to_list(X), V2 = list_to_binary(V1), V3 = capitalize_binary(V2), V4 = binary_to_list(V3), binary_to_atom(V4).
def capitalize_atom(X) do X |> atom_to_list |> list_to_binary |> capitalize_binary |> binary_to_list |> binary_to_atom end
However, I think if any of the functions in the series take more than one argument, things could quickly get cumbersome. Witness the discussion on Joe’s blog, Joe’s recent tweets suggesting improvements, and the caveat in the elixir documentation.
The simple case above could be done in erlang with a fold:
capitalize_atom(X) -> lists:foldl(fun(F, Acc) -> F(Acc) end, X, [fun atom_to_list/1, fun list_to_binary/1, fun capitalize_binary/1, fun binary_to_list/1, fun binary_to_atom/1] ).
Granted, this is possibly even yuckier than Joe’s version, but using a fold or, more generally, writing a function that walks through a list of funs, gives the possibility of handling errors, using some kind of context dictionary as the passed argument, etc.
I think elixir’s “|>” looks more general than it usefully is.
In erlang, variables must begin with a capital letter; atoms need special marking only if they start with a capital letter, or contain certain characters, in which case the atom is enclosed in single quotes. In erlang, module names and function names are atoms.
In elixir, variables don’t seem to need any special marking. Atoms /sometimes/ need to be marked with an initial colon. Unless the atom is a function or module name. Unless the module/function is from the erlang standard library. I might have that wrong.