Docs on data type comparisons should be updated to reflect proper usage and avoidance of "nesting"
aflansburg opened this issue · 1 comments
The Getting Started > Basic Operators page states:
The reason we can compare different data types is pragmatism. Sorting algorithms don’t need to worry about different data types in order to sort. The overall sorting order is defined below:"
number < atom < reference < function < port < pid < tuple < map < list < bitstring
However, if you write something (naive example) such as:
1 < :one < {1,2,3} < [1,2,3] < "123"
The output is true, however the interpreter complains (for each comparison operation):
warning: Elixir does not support nested comparisons. Something like
x < y < z
is equivalent to
(x < y) < z
which ultimately compares z with the boolean result of (x < y). Instead, consider joining together each comparison segment with an "and", for example,
x < y and y < z
You wrote: 1 < :one < {1, 2, 3} < [1, 2, 3] < "123"
Further information is referenced in a link to https://hexdocs.pm/elixir/operators.html - however, there is no mention of proper usage or avoidance of the above improper "nesting" usage. While the warning from the interpreter provides plenty of insight, it would be good to include proper usage either in these docs or, if out of scope for a Getting Started type page, provide them in the language API documentation on HexDocs
Hi @aflansburg, thanks for the report! The ordering in that section is not meant to be taken literally, but I can see how that can be confusing. I will try to rephrase it.
Regarding "nesting" usage, I am usually not a big fan of telling people what something does not work, because the expectation if nesting works or not will depend on the user background. So as long as we don't confuse them, which is what I hope to change, then we should be good to go (and the warning will address the other cases!).