# When == is passed arguments of different type

**URL:** <https://discourse.julialang.org/t/when-is-passed-arguments-of-different-type/93516>\
**Category:** Internals & Design\
**Created:** [January 25, 2023, 4:02pm UTC](https://discourse.julialang.org/t/when-is-passed-arguments-of-different-type/93516 "2023-01-25T16:02:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaboaf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaboaf/32/44335_2.png) [@jaboaf](https://discourse.julialang.org/u/jaboaf)\
**Post date:** [January 25, 2023, 4:02pm UTC](https://discourse.julialang.org/t/when-is-passed-arguments-of-different-type/93516/1 "2023-01-25T16:02:56Z")

</div>

just wondering about the design choice where, for example,  
julia\> Int8(1)==Int64(1)  
true  
julia\> typeof(Int8(1))==typeof(Int64(1))  
false

why is it that way ? or why was that choice made ? as opposed to, for example,  
julia\> Int8(1)==Int64(1)  
false

p.s. long time julia user first time poster; I appreciate everyone contributing to the design

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 25, 2023, 4:07pm UTC](https://discourse.julialang.org/t/when-is-passed-arguments-of-different-type/93516/2 "2023-01-25T16:07:09Z")

</div>

I would think that that’s because the integer 1 is exactly the same value whether it is stored as an 8-bit of 64-bit integer? So the question is whether the exact same number can be represented with the numerical type. Compare to e.g.:

```julia
julia> Float64(1) == Float32(1)
true

julia> Float64(0.3) == Float32(0.3)
false

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 25, 2023, 4:12pm UTC](https://discourse.julialang.org/t/when-is-passed-arguments-of-different-type/93516/3 "2023-01-25T16:12:00Z")

</div>

Also I would add there is `===` for a comparison involving the type altogether.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [January 25, 2023, 7:05pm UTC](https://discourse.julialang.org/t/when-is-passed-arguments-of-different-type/93516/4 "2023-01-25T19:05:28Z")

</div>

The basic gist is in the [== docs](https://docs.julialang.org/en/v1/base/math/#Base.:==), emphasis mine:

> Generic equality operator. Falls back to [`===`](https://docs.julialang.org/en/v1/base/base/#Core.:===). Should be implemented for all types with a notion of equality, **based on the abstract value that an instance represents.** For example, all numeric types are compared by numeric value, ignoring type.

This isn’t a trivial matter, and it really cuts to the core of what makes Julia’s generic functions (and multiple dispatch) work. Doing this consistently is quite the feat, especially as it has impacts on other comparisons and hashing… and it needs to play nicely with promotion and wonky floating point semantics and more.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [January 25, 2023, 7:35pm UTC](https://discourse.julialang.org/t/when-is-passed-arguments-of-different-type/93516/5 "2023-01-25T19:35:08Z")

</div>

~~It seems Jeff and Stefan want to get rid of this misfeature in 2.0.~~

> <https://github.com/JuliaLang/julia/issues/40717>
>
> I often make mistakes by using \`==\` between two objects that I shouldn't be comp…aring, such as comparing a number to an array of numbers or comparing a string to a character. The \`==\` comparison evaluates to \`false\`, but it wasn't the comparison I meant to make. For example,
> 
> \`\`\`jl
> a = \[1,2,3\]
> b = 5
> 
> 
> if a == b; # evaluates to false
> \`\`\`
> 
> I wish there were an equality-checking operator that would give an error for incompatible types, like in the example above. I'm not sure if the right implementation would be 
> 
> \- check if they have exactly the same type
> \- check some \`\<:\` subtyping relationship
> \- check if they can \`convert\` into each other's types
> \- check if they can \`promote\` into a shared type
> 
> or something else, or multiple different operators for different purposes. There are plenty of equals-like operators in unicode (eg ≟, ≐). Regardless, the current situation where my mistakes result in silent \`false\`s causes me problems too often for comfort; I would much rather get an explicit error that I can correct. 
> 
> What would be a good solution here?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [January 25, 2023, 7:58pm UTC](https://discourse.julialang.org/t/when-is-passed-arguments-of-different-type/93516/6 "2023-01-25T19:58:12Z")

</div>

That’s not what the OP was asking about — `Int8(1) == 1 == 1.0 == 1/1` is not generally considered a misfeature. That issue is about comparing types that don’t have a meaningful relation, like `1 == Dict("hello"=>[])`.

---

<div class="post-metadata">

**Author:** ![melonedo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/melonedo/32/15503_2.png) [@melonedo](https://discourse.julialang.org/u/melonedo)\
**Post date:** [February 19, 2023, 8:45am UTC](https://discourse.julialang.org/t/when-is-passed-arguments-of-different-type/93516/7 "2023-02-19T08:45:27Z")

</div>

Base.jl has this line:

```julia
==(x, y) = x === y

```

So that means if there is no defined method to compare two values, it falls back to identity comparison. You can check that with `methods(==)`:

```plaintext
julia> methods(==)
# 171 methods for generic function "==":
[1] ==(r::Union{StepRange{T}, StepRangeLen{T, T}}, s::Union{StepRange{T}, StepRangeLen{T, T}}) where T in Base at range.jl:1116
[2] ==(r::T, s::T) where T<:Union{LinRange, StepRangeLen} in Base at range.jl:1113
...
[161] ==(y::Int64, x::Float32) in Base at float.jl:450
[162] ==(y::Int64, x::Float64) in Base at float.jl:450
[163] ==(x::T, y::T) where T<:Number in Base at promotion.jl:477
[164] ==(x::Number, y::Number) in Base at promotion.jl:427
...
[169] ==(x) in Base at operators.jl:1135
[170] ==(w, v::WeakRef) in Base at gcutils.jl:5
[171] ==(x, y) in Base at Base.jl:116

```
