How to test type equality

So the documentation says:

Be careful with type equality
You generally want to use isa and <: for testing types, not ==. Checking types for exact equality typically only makes sense when comparing to a known concrete type (e.g. T == Float64), or if you really, really know what you’re doing.

But: isa does not test the equality of the arguments, it tests the type of one of the arguments against the other argument.

So, this comparison of types doesn’t work:

julia> Float64 isa Float64
false

Is that what the intent of using isa truly is? If so, how does one test the equality of types?

1 Like

I think the documentation is trying to say that you should do:

1.0 isa Float64

or

typeof(1.0) <: Float64

instead of

typeof(1.0) == Float64
1 Like

Okay, I get that. But what if I have x = Float64 and I want to check whether x is Float64?

In that context it doesn’t matter that it’s a type — you’re just treating it as a value. So you can use ==. This is the case the documentation describes as comparing to a known concrete type.

julia> Float64 == Float64
true
1 Like

Aha, makes sense. Thanks.

And just as a note, equality between types is implemented as ==(T1::Type, T2::Type) = T1 <: T2 && T2 <: T1.

8 Likes