I made up function inside Equatable extension which uses pattern matching to compare equality, and this does not warn me about long time to type check.
What could this differ from==
operator used explicitly?
There are a few factors here:
- There are a lot more overloads for the
==
operator than for theequals
method that you wrote. Looking at the debug output of the type checker tells me there are 73 overloads of==
in the standard library alone. If you wrote 70 overloads ofequals
, it'd probably exhibit the same performance issues as==
- The
equals
function you wrote is a method, which means you call it directly on an instance that conforms toEquatable
, e.g.object.equals(otherObject)
. Methods are a lot easier for the compiler to figure out, because the compiler only needs to consider overloads on the base type that you called the method on. With operators, the compiler has to consider every overload of==
regardless of what type or protocol it's declared on, because operators are always called like global functions.
EDIT:
To answer your question about why pattern matching type checks faster, the answer is the same as 1. above: there are a lot more overloads of ==
(~70 overloads) than there are of the pattern matching operator ~=
(5 overloads)