maxguzenski

maxguzenski

Nil > 25 returns true – is this expected behavior?

Hi everyone,

I’m using Elixir 1.18.4 with OTP 27 (I’ve tested this across multiple versions of both Elixir and Erlang), and surprisingly, the expression nil > 25 returns true. Is this correct?

Both ChatGPT and Claude stated that this should not be possible — that a comparison like this should raise an ArgumentError, as nil cannot be compared to numbers using > or <. They even suggested that something might be wrong with my machine.

However, I tested this on https://playground.functional-rewire.com/ as well, and I got the same result: nil > 25 returns true.

So my question is:

Has it always been this way, or is this a recent change/bug?

Thanks in advance!

First Post!

sodapopcan

sodapopcan

Yes, comparison operators can be used across types. As for why nil > 25 returns true, see the good ol’ docs. Of note: nil is simply syntactic sugar for the atom :nil (you can prove it with iex> nil == :nil).

Most Liked

sabiwara

sabiwara

Elixir Core Team

While there is no runtime warning for the reasons explained above, it’s worth noting that the new type system might be able to catch it and warn at compile time when it is sure types are disjoint:

comparison between distinct types found:

    nil > 25

given types:

    nil > integer()

While Elixir can compare across all types, you are comparing across types which are always disjoint, and the result is either always true or always false
18
Post #8
sodapopcan

sodapopcan

You sound like one of those people who actually know how to use a dynamic programming language properly :grin: Also, we’re in the BEAM world here so I believe you meant “behaviour” :wink:

As grumpy old man jumping on the opportunity to rant this has been fairly common recently, “common” meaning this is the ~7th time in a couple of months I’ve seen a question online where people said they just asked AI, and not just for Elixir questions! I’m sure it’ll all be “fixed” soon enough, but I still enjoy reading docs.

Back on track, another little gotcha is that true and false are also sugar for :true and :false, so when you throw nil into the mix and compare them it’s done so alphabetically. I know a couple of people who got tripped up by this because they assumed that since true is greater than false that false would be greater than nil, but it’s not. Though again, if you aren’t abusing dynamic languages then you’ll never run into this :smiley:

k, I’m off to go yell at a cloud.

adamu

adamu

software developers in particular are prone to being convinced by these hazards and few in the field seem to have ever had that “oh my, I can’t always trust my own judgement and reasoning” moment

Last Post!

dimitarvp

dimitarvp

It is, but it’s also predictable, I was a teenager when I already expected it (30 years ago). There’s just something about Homo Sapiens that makes them want to believe in magic even with a mountain of evidence that it doesn’t exist.

It was only a matter of time. And, well, the time has come.

And back on topic, not checking after the LLM is a recipe for disaster. That’s also quite obvious. And for whomever it isn’t obvious, it’ll become obvious soon enough. :grin:

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

Other popular topics Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement