-
Notifications
You must be signed in to change notification settings - Fork 407
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 java.time._ Instances For The JVM #734
Conversation
This commit adds Scalacheck instances for almost all of the types in the `java.time` package and sub-packages, for which meaningful definitions seem to exist. Almost all implemented types have `Gen.Choose` instances. `Shrink` instances were defined for the types that seemed to have a reasonably straight forward definition. And all implemented types have an `Arbitrary` instance. The instances were built against JRE 8, though I'm not aware of any changes between the JRE definitions and the current (JRE 15) definitions. The types are scoped to `org.scalacheck.time`, with the intent that `import org.scalacheck.time._` would bring them all into the implicit scope. It is likely worth discussing why these types are being added. Scalacheck instances for `java.time` types are hardly new. There are several libraries which provide some instances for some `java.time` types. There is however, no library of which I am aware, which provides a relatively complete implementation of these types, e.g. instances for most types, with full range support, and `Choose` instances. If such a library already exists, then I am more than happy to withdraw this PR. Further, `java.time` types have a relatively large user base in the Scala community. By hosting instances in Scalacheck proper, we increase exposure to these instances and lessen the likelihood that developers will attempt to re-invent the wheel, or just use stub instances, e.g. `Gen.const(LocalDate.now)`. While these types are currently scoped to the JVM only, it is arguable that they could be provided for ScalaJs and ScalaNative too. Both ScalaJs and ScalaNative have, or are working on, replacement API compatible implementations. However adding support in Scalacheck proper for this would require adding dependencies and is thus punted to a later discussion for now.
I've encoded trait which brings the implicits into scope as such, package object time extends time.JavaTimeInstances Though I acknowledge that several other popular encodings exist. For example, package object time {
object implicits extends time.JavaTimeInstances
} or package object time {
object syntax {
object all extends time.JavaTimeInstances
}
} If the maintainers have any preference's here, I'm more than happy to make changes to that structure. |
Also totally happy to move these if they would be better in another module or to close the PR if they aren't wanted. :) |
Looks like Scala is missing an |
Scala <= 2.12 gets confused because some of the `java.time._` types implement `Comparable` on their super type. Thus this commit adds explicit orphaned `Ordering` instances for them, but only for Scala <= 2.12 and only in jvm tests.
Fixed the compilation issues on Scala < 2.13. It was having issues resolving |
This is a nice contribution. Would a better home for this be https://github.com/47degrees/scalacheck-toolbox though? |
@ashawley So that's a bit of a tricky question. I considered proposing this PR there, but I decided not to do so for these reasons,
|
Anything I can do to move this along? Or should I close it? Or do people just need more time to think it over? |
I'm leaning towards inclusion. @ashawley, do you veto it? |
is there somebody at 47 Degrees we should solicit an opinion from? (@alejandrohdezma, perhaps?) |
FWIW, there's also this library: https://github.com/tmccarthy/intime |
@larsrh No, I wasn't trying to veto. I was attempting, but wasn't convincing enough, to facilitate getting this contributed to a library elsewhere in the ScalaCheck ecosystem. In the spirit of what Richard wrote at #351 (comment) about keeping the library minimal and because we don't have a lot of capacity for maintaining the project, we should probably lean towards leaning the capabilities of the library to some definition, like Scala its standard library and some Java primitives. |
cc @tmccarthy |
Just providing my humble opinion since my library was tagged above. This seems like a nice addition that provides basically the same functionality as the Probably the only value |
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.
Thanks @tmccarthy for chiming in and @isomarcte for the contribution. I'm a friend of optimistic merging, so I'm suggesting to do that here. If noone objects, I'll push the merge button in a few days.
If contributing this to other ScalaCheck libraries isn't ideal, then adding to ScalaCheck core seems fine. Time values aren't Java primitives, but they are used enough that it seems like a valid exception. |
This is very smart, but I would rather they just be added to the library the same way as other types. I don't think we need to add the namespace. It might be something to consider in the future. |
@ashawley just to clarify, are you saying you'd like the instances to be mixed into the companion objects for the various typeclasses, e.g. If so, do you want me to remove the |
Since it's private and if it doesn't get in the way, then I don't see any reason not to use the |
@ashawley I'll update it to reflect those changes. |
This commit breaks up the `JavaTimeInstances` trait into 4 separate traits, one for each typeclass. Each typeclass's companion object now extends the corresponding trait. This has the effect of automatically bringing the instances into implicit scope on the JVM. For ScalaJs and Scala Native, the traits are just empty stubs.
Thanks a ton @isomarcte! |
This commit adds Scalacheck instances for almost all of the types in the
java.time
package and sub-packages, for which meaningful definitions seem to exist.Almost all implemented types have
Gen.Choose
instances.Shrink
instances were defined for the types that seemed to have a reasonably straight forward definition. And all implemented types have anArbitrary
instance and aCogen
instance.The instances were built against JRE 8, though I'm not aware of any changes between the JRE definitions and the current (JRE 15) definitions.
The types are scoped to
org.scalacheck.time
, with the intent thatimport org.scalacheck.time._
would bring them all into the implicit scope.It is likely worth discussing why these types are being added. Scalacheck instances for
java.time
types are hardly new. There are several libraries which provide some instances for somejava.time
types. There is however, no library of which I am aware, which provides a relatively complete implementation of these types, e.g. instances for most types, with full range support, andChoose
instances. If such a library already exists, then I am more than happy to withdraw this PR.Further,
java.time
types have a relatively large user base in the Scala community. By hosting instances in Scalacheck proper, we increase exposure to these instances and lessen the likelihood that developers will attempt to re-invent the wheel, or just use stub instances, e.g.Gen.const(LocalDate.now)
.While these types are currently scoped to the JVM only, it is arguable that they could be provided for ScalaJs and ScalaNative too. Both ScalaJs and ScalaNative have, or are working on, replacement API compatible implementations. However adding support in Scalacheck proper for this would require adding dependencies and is thus punted to a later discussion for now.