double equal operator not working as defined

Using Elixir 1.12.3.
New to this programing paradigm. When I specify “8” == 8 in iex it returns false. I indeed get the same result when I specify “8” === 8. According to the Elixir/Erlang definition the double == operator is supposed to type cast the string to an integer and return true. Any insights would be greatly appreciated.

Respectfully,
Ideprize

The observed behavior is correct AFAIK

Where did you find that information? Maybe it mentions floats specifically?

1 Like

== vs === behaviour differs ONLY for numerics (aka integers and floats). Otherwise it always is strict with a type and there is never any conversion between types.

2 Likes

Thanks for the response hauleth. Are you saying that “==” will accept integer and float equality or “===” will accept integer and float equality, i.e 8 == 8.0 is true or 8 === 8.0 is true. Note this is different from Javascript and I must admit I should not have assume coincidence. My bad.

Respectfully,
Ideprize

According to the documentation:

The only difference between == and === is that === is strict when it comes to comparing integers and floats:

iex> 1 == 1.0
true
iex> 1 === 1.0
false

!= and !== act as the negation of == and ===, respectively.

Base Elixir will never perform any kind of implicit type coercion, so this is the only exception when it comes to comparisons.

There is another case though not related to == which is falsyness and truthyness. nil and false are the only falsy values in Elixir, everything else is truthy. Meaning that in this snippet:

if val do
  IO.puts("Truthy!")
else
  IO.puts("Falsy!")
end

It will print Falsy! if val is either nil or false, and for any other value it will print Truthy! But still nil == false will return false.

5 Likes