SLC: more `CanEqual` instances in the standard library

Hi,

I’d like to further push adoption of the strict-equality feature by making it easier to use. One problem is that many Java standard library types currently don’t have CanEqual instances, and I’d like to fix that by providing them.
The types I’m proposing are:
java.util.UUID, java.net.URI, Instant, LocalDate, LocalTime, LocalDateTime, OffsetTime, OffsetDateTime, ZonedDateTime, Year, YearMonth, MonthDay, java.time.Duration, scala.concurrent.duration.Duration (and thereby also FiniteDuration), Period, ZoneId (and thereby also ZoneOffset), DayOfWeek, TimeUnit, Try[A].

A branch with these instances can be found here:

Note that this will also make codebases safer that don’t even use strict-equality, because even with strict-equality disabled, the Scala compiler will prevent you from making a == comparison if one side has a reflective CanEqual instance and the other doesn’t match.

6 Likes

looks great, please open a PR :slight_smile:

my wish would be that we can have java.time in js/native’s standard library too

Are there concerns this would break backward compat ?

1 Like

Only in the sense that it would prevent code from compiling that is broken in the first place. When strictEquality is disabled, and a reflexive CanEqual instance is available for one of the sides, then it’ll continue to compile if the other type is a supertype of it, so you can still compare, say, LocalDate to Any. But it won’t allow you to compare LocalDate to Option[LocalDate], and that’s a feature, not a bug.

2 Likes

But any type can be widened to Any, so how does it tell ?

Do you have a concrete example of the non-strict-equality error, so I can wrap my head around it ?

scala> trait Foo derives CanEqual
// defined trait Foo
                                                                                                                                                                                                                                                                                                                                                                                                                                    
scala> def f(x: Foo) = x == x
def f(x: Foo): Boolean
                                                                                                                                                                                                                                                                                                                                                                                                                                    
scala> def g(x: Foo) = x == 0
-- [E172] Type Error: ----------------------------------------------------------
1 |def g(x: Foo) = x == 0
  |                ^^^^^^
  |              Values of types Foo and Int cannot be compared with == or !=
1 error found
                                                                                                                                                                                                                                                                                                                                                                                                                                    
scala> def h(x: Foo, y: Any) = x == y
def h(x: Foo, y: Any): Boolean

Thanks I see now

I misunderstood the system previously !
(I thought a == b and a == (b: Any) would be equivalent)

Therefore I agree the backwards compat issue is unlikely, since it would require an equality which is never satisfied at runtime (or abuse of asInstanceOf)