Elixir's biggest gotchas?

3 things I have noticed when giving courses[*] that people coming from a non-functional background can have problems with are immutable data, pattern matching, and recursion. But once you grasp them you never want to go back.

Robert

[*] It has mainly been erlang courses, and now elixir courses, but at this level they are the same.

10 Likes

I switch-hit between ruby and elixir all the time, and Iā€™m constantly lamenting the lack of pattern matching now. I really donā€™t get how I did this for so long without it!

7 Likes

Iā€™m not sure if this is a gotcha, but I find it difficult to read backtraces due to macros and/or anonymous functions.

Remembering the scoping rules. Iā€™ve given up on that and now just assume everything creates a new scopeā€¦ that seems to be the direction things are headed anyway.

Ruby muscle memory and spending minutes looking for where I forgot to type do.

The precedence of |> (also due to Ruby muscle memory and an unconscious desire for special syntax for functions whoā€™s last argument is a function). Iā€™ve definitely learned to heed the compilerā€™s warnings on this oneā€¦ :slight_smile:

3 Likes

TIL you can pipe into an anonymous functionā€¦ even though the syntax may be a little awkward.

Iā€™m not sure if this qualifies as a ā€˜gotchaā€™ per se, but I personally struggled with getting a complete grasp of the syntax. I donā€™t mean being able to use the syntax productively but rather the process unraveling the different macros and sugar and the different ways they can be represented.

Keyword lists and macros have already been mentioned above, which is really what I think this boils down to, but Iā€™m talking about things like:

def add(x, y) do
  x + y
end

Being the same as:

def add(x, y), do: x + y

Which can be:

def add(x, y), [do: x + y]

Which is really:

def add(x, y), [{:do, x + y}]

Which can be:

def(add(x, y), [{:do, x + y}])

And now itā€™s revealed that what looked like a ā€˜standardā€™ function definition statement with some parameters and a function body is actually itself a call to a function.

I think there is a piece that Iā€™m still missing with the add(x, y) definition really being something more akin to a value than a call, and when/how that conversion from a call to a value gets handled.

This isnā€™t necessarily a hurdle that you need to get over in order to become productive, but before you understand whatā€™s going on behind the syntax little things can bleed through that may seem strange.

6 Likes

Thanks

That was an interesting post and I learned something new today.

I sort of agree with you. My biggest gotcha with elixir is that there
are so many ways of doing the same thing and I have the habit of picking
the one which shots me in the foot!

Many of the examples donā€™t work. Many of the github projects, beyond the basic functionality that ships with Elixir itself, are abandoned or donā€™t work. Itā€™s a huge problem when trying to evaluate how robust and mature the Elixir eco-system is for commercial adoption.

EDIT: Since I was asked for examples, I will list them here.

http://blog.rh-flow.de/2016/01/19/parallel-processing-of-csv-in-elixir-and-golang/
https://github.com/jordipolo/dataframe

EDIT 2: My interest is in building a multi-node system that can do distributed processing of data in a ruby-style functional programming language. Data would enter this system via CSVs and Excel files (not just databases) and do extensive number crunching on all cores.

2 Likes

Can you please be a bit more constructive and show us some examples of ā€œbasic things on github that donā€™t workā€?

When I came to elixir, roughly at the edge of 1.0, yes I had this feeling as well, since I nearly found pre-1.0 stuff that didnā€™t work anymore with 1.0. But this happens.

This happened to me with rust as well, and also happens with ruby every now and then after there was a new ā€œmajorā€ (in the sense of massive changes, not versioning schema) update.

Today, most of the old tutorials simply vanished from the net, or they have been updated. Or one can simply see by the date, that they probably wonā€™t help anymore.

Youā€™re not missing any piece, except for: def/2 is not a function, itā€™s a macro, thats why add(x, y) is not a call to a function, and yes the AST for itā€™s definition. You can give it a check on Kernel.def/2 documentation.

BTW, for me, this is one of the most awesome gotchas about Elixir: documentation as a ā€œfirst class citizenā€, if you allow me to pervert a little bit the ā€œfirst class citizenā€ term. However, elixir documentation is just awesome, and having them offline, and easily, with the h/1 function on iex is just wonderful. This made my learning experience a lot faster and softer!

5 Likes

Upvote for verging cargo-cult :smiley:

there are two points here that could be added to Elixir Gotchas and Common Issues Wiki

'foo' != "foo": Elixir's biggest gotchas? - #10 by josevalim
keyword lists are just sugar: Elixir's biggest gotchas? - #12 by bbense

1 Like