DEV Community

Sri Kadimisetty
Sri Kadimisetty

Posted on

 

Ordering datatypes in elixir

Here's an elixir factoid that's somewhere between mildly amusing and slightly bizarre, if you haven't come across it yet —

iex(1)> 10 < :foo # Comparing an integer to an atom
true
iex(2)> f = fn () -> :foo end
...
iex(3)> f < [1,2] # Comparing a function to a list
true
Enter fullscreen mode Exit fullscreen mode

That is all expected behavior. Apparently different datatypes are allowed to be compared as they are all ordered for reasons to do with keeping sorting algorithms practically efficient or some such. Here's the expanded precedence order:

number < atom < reference < function < port < pid < tuple < map < list < bitstring

and the relevant doc with more nitty gritties.

Later now!

Top comments (0)