Skip to content
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

Currency instances #4564

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

massimosiani
Copy link

Addresses #4562.
I followed the steps outlined here #3910 (comment), there's no Currency in scala js/native tough.

build.sbt Outdated Show resolved Hide resolved
Comment on lines +33 to +36
implicit private val arbitraryCurrency: Arbitrary[Currency] = Arbitrary(
Gen.oneOf(Currency.getAvailableCurrencies().asScala)
)
implicit private val cogenCurrency: Cogen[Currency] = Cogen[String].contramap(_.getCurrencyCode())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, it would not hurt if we put these instances into cats.laws.discipline.arbitrary to make them available for everyone.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wrapping my head around the dependency graph here. Doesn't the laws module depend on this one? If we want to add these instances to kernel-laws, is there a way to add jvm-only code without moving too many files (kernel-laws is defined as CrossType.Pure)? Sorry not very used to cross compilation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it is quite confusing. If it is not possible to do it in a simple way, let's keep it private in the tests for now then.

@@ -48,4 +49,8 @@ private[cats] object scalaVersionSpecific {
that: T
)(implicit w1: A => TraversableLike[El1, Repr1], w2: T => IterableLike[El2, Repr2]) = (a, that).zipped
}

implicit class setExtension[A](private val a: A) extends AnyVal {
def asScala(s: java.util.Set[A]): mutable.Set[A] = `set asScala`(s)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@armanbilge any better idea?

@massimosiani
Copy link
Author

Is the FutureSuite kind of flaky? I don't think it fails due to my changes.

@satorg
Copy link
Contributor

satorg commented Dec 23, 2024

Hi @massimosiani , sorry for the long reply. Looks like your PR got buried under a pile of others, although it looks pretty mature. Would you be interested to move it forward? In such a case, would you update it to the current main and thereby trigger CI checks to run again, please? Thank you!

@massimosiani
Copy link
Author

Hi @satorg, of course! Thanks for the update, I'll do it ASAP.

@@ -21,7 +21,8 @@

package cats.kernel.compat
import scala.annotation.{Annotation, StaticAnnotation}
import scala.collection.{IterableLike, TraversableLike}
import scala.collection.{mutable, IterableLike, TraversableLike}
import scala.collection.convert.ImplicitConversionsToScala.*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import scala.collection.convert.ImplicitConversionsToScala.*
import scala.collection.JavaConverters._

see
https://github.com/scala/scala/blob/13f19f4d5f9c036019b671a53a788bbaed67adf8/src/library/scala/collection/convert/ImplicitConversions.scala#L146-L149

then you can call a conversion method explicitly, e.g.: asScalaSet(s), asScalaWhatever(x), etc.

Also please note that Cats hasn't switched to Scala3 import style yet, see discussion in #4677.

Comment on lines 55 to 57
implicit class setExtension[A](private val s: java.util.Set[A]) extends AnyVal {
def asScala: mutable.Set[A] = `set asScala`(s)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This extension method is only used for Gen.oneOf in tests, and the latter only requires Iterable. So I'd suggest to go with the minimally required type here as well:

Suggested change
implicit class setExtension[A](private val s: java.util.Set[A]) extends AnyVal {
def asScala: mutable.Set[A] = `set asScala`(s)
}
implicit class iterableExtension[A](private val s: java.lang.Iterable[A]) extends AnyVal {
def asScala: Iterable[A] = iterableAsScalaIterable(s) // assuming import scala.collection.JavaConverters._
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides, if this extension method is for the sake of testing only, I wonder if it'd make sense to move it from "main" to "test" scope?

@@ -35,4 +37,8 @@ private[cats] object scalaVersionSpecific {
implicit class iterableOnceExtension[A](private val io: IterableOnce[A]) extends AnyVal {
def reduceOption(f: (A, A) => A): Option[A] = io.iterator.reduceOption(f)
}

implicit class setExtension[A](private val s: java.util.Set[A]) extends AnyVal {
def asScala: mutable.Set[A] = SetHasAsScala(s).asScala
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt that SetHasAsScala is a part of any API. Not mention this call can allocate a class instance without a reason. I'd probably give a shot to scala.jdk.javaapi.CollectionConverters here, e.g.:

import scala.jdk.javaapi.CollectionConverters

...

def asScala: Iterable[A] = CollectionConverters.asScala(x)

@@ -35,4 +36,8 @@ private[cats] object scalaVersionSpecific {
implicit class iterableOnceExtension[A](private val io: IterableOnce[A]) extends AnyVal {
def reduceOption(f: (A, A) => A): Option[A] = io.iterator.reduceOption(f)
}

implicit class setExtension[A](private val s: java.util.Set[A]) extends AnyVal {
def asScala: Iterable[A] = CollectionConverters.asScala(x)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def asScala: Iterable[A] = CollectionConverters.asScala(x)
def asScala: Iterable[A] = CollectionConverters.asScala(s)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants