Open stereotype441 opened 6 months ago
Agree.
We do allow overriding operator==
, so we should also use the overridden type in the minuscule amount of cases where someone does that. (But… like why? And like don't!)
The other alternative is to remove coercion as well, but this is a statically decided behavior, so it's cost-free in most cases (because K?
will be Object?
in almost every case), and in the hypothetical case where we know that calling is going to throw if we don't coerce, we might as well do the consistent thing.
(We still can't change the return type to the declared return type, even if we can now have extension types as subtypes of bool
. Unless Erik's proposal of allowing extension type return types for fx async
functions can be extended to operator==
.)
The analyzer and front end currently use the following rules to perform type inference for equality expressions (expressions of the form
e1 op e2
, whereop
is either==
or!=
):e1
is type inferred in context_
, producingm1
with static typeT1
.K
be static type of the single argument accepted byT1.operator==
.e2
is type inferred in context_
, producingm2
with static typeT2
.m
as follows:T2 <: K?
, letm
bem1 op m2
.C
exists that coerces typeT2
toT2'
, andT2' <: K?
, then letm
bem1 op C(m2)
.e1 op e2
ism
, with static typebool
.It seems odd to me that the type
K?
is used for coercions but not to supply a context when type inferringe2
. This matters if a user decides to declare anoperator==
with a covariant argument type. For example:This code is rejected by both the analyzer and front end, with the error message:
However, if
C.operator==
is replaced with any other user definable operator, then the code is accepted:This seems unnecessarily inconsistent. I think we should change the third bullet in the type inference rules to be:
e2
is type inferred in contextK?
, producingm2
with static typeT2
.This would make type inference for
operator ==
more consistent with other operators.