An Observation of Unexpected Results

This is not a question, just an observation that is worth noting…

I was getting ‘incorrect’ information on one of my web pages and I discovered this anomoly in Elixir:

iex(1)> 1 > nil
false
iex(2)> 1 < nil
true
iex(3)> -1 > nil
false
iex(4)> -1 < nil
true

I was expecting any non-null value to be greater than null (nil). Apparently that isn’t the case, so just be careful when your code requires a comparison against null (nil).

Notably, this is covered in the operator docs: https://hexdocs.pm/elixir/operators.html#comparison-operators

4 Likes

nil is just an atom as every other atom, with the exception that you can leave off the colon. Nothing more, nothing less.

3 Likes