-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Kernel instances for java-time datatypes #3910
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just in kernel? Should this have forwarders so cats imports get this automatically or am I missing them?
trait JavaTimeInstances { | ||
|
||
implicit final val catsKernelStdOrderForduration | ||
: Hash[Duration] with Order[Duration] with CommutativeGroup[Duration] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we aren't testing the commutative group are we? I don't see where those laws are checked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, the problem I am having with some of the tests is that, if it generates very large values, it is causing overflows. Not sure how we should address this problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was able to fix it by defining a generator for the Duration
class itself. It is just copied from the one for FiniteDuration
. @johnynek Would that be enough?
Once this is compiling on ScalaJS, we need to double-check that downstream projects are not forced to take on the Java Time shim dependency. |
@@ -25,6 +25,7 @@ trait AllInstances | |||
with FutureInstances | |||
with HashInstances | |||
with InvariantMonoidalInstances | |||
with JavaTimeInstances |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just in kernel? Should this have forwarders so cats imports get this automatically or am I missing them?
@ChristopherDavenport IIUC this should add the instances to the core itself too
In [Issue 3766](#3766) it was proposed to add java.time instances to cats itself, considering that `java.time` is just as standard as UUID or the Scala library. Within the companion objects of the typeclasses `Eq`, `Order`, and `Hash`, we define new "mixin" traits that provide implementations of each type-class based on the Any-hash, Any-eq, or Comparable. These are only used to define simple instances of many typeclasses. With that, each class in `java.time` has an instance of the kernel typeclasses written in two lines, and they all fit in one file. We also add instances of Show in the `core` module, which are just based on the `toString` method. - Duration Test: add a generator for durations
The model followed here is similar to that of |
This is super super important. Time is an enormous increase to JS file size where the prospect of having it in a JS file is generally referred to as crazy by the community. |
I was pointed here by @joroKr21 in #3871 (comment). Actually, if @diesalbla or someone else wants to, I think we can move forward with this. Here's how:
What this achieves:
Risks:
|
Issue 3766 proposed integrating
cats-time
intocats
, and adding instances for the data types injava.time
, which is after all just as standard as the Scala library or UUID.All the instances, as defined in
cats-time
, just use the natural equality, hash code, or comparable implementations defined in the Java library. To ease the definition, within the companion objects of the typeclassesEq
,Order
, andHash
, we define new "mixin" traits that provide implementations of each type-class based on the Any-hash, Any-eq, or Comparable.With that, each class in
java.time
has an instance of the kernel typeclasses written in two lines.