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 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ lazy val kernelLaws = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.settings(testingDependencies)
.jsSettings(commonJsSettings)
.jvmSettings(commonJvmSettings)
.jvmSettings(libraryDependencies += "org.scala-lang.modules" %% "scala-collection-compat" % "2.11.0" % Test)
massimosiani marked this conversation as resolved.
Show resolved Hide resolved
.nativeSettings(commonNativeSettings)

lazy val algebraSettings = Seq[Setting[?]](
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/scala-2.12/cats/instances/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ abstract class AllInstancesBinCompat
with AllInstancesBinCompat6
with AllInstancesBinCompat7
with AllInstancesBinCompat8
with AllInstancesBinCompat10

trait AllInstances
extends AnyValInstances
Expand Down Expand Up @@ -94,3 +95,5 @@ trait AllInstancesBinCompat7 extends SeqInstances
trait AllInstancesBinCompat8 extends DeadlineInstances

trait AllInstancesBinCompat9 extends InvariantInstances with InvariantInstancesBinCompat0

trait AllInstancesBinCompat10 extends CurrencyInstances
3 changes: 3 additions & 0 deletions core/src/main/scala-2.13+/cats/instances/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ abstract class AllInstancesBinCompat
with AllInstancesBinCompat7
with AllInstancesBinCompat8
with AllInstancesBinCompat9
with AllInstancesBinCompat11

trait AllInstances
extends AnyValInstances
Expand Down Expand Up @@ -99,3 +100,5 @@ trait AllInstancesBinCompat8 extends InvariantInstances
trait AllInstancesBinCompat9 extends DeadlineInstances

trait AllInstancesBinCompat10 extends InvariantInstancesBinCompat0

trait AllInstancesBinCompat11 extends CurrencyInstances
29 changes: 29 additions & 0 deletions core/src/main/scala/cats/instances/currency.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package cats
package instances

import java.util.Currency

trait CurrencyInstances extends cats.kernel.instances.CurrencyInstances {
implicit val catsStdShowForCurrency: Show[Currency] = Show.fromToString[Currency]
}
40 changes: 40 additions & 0 deletions kernel-laws/jvm/src/test/scala/cats/kernel/laws/JvmLawTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package cats.kernel
package laws

import cats.kernel.instances.currency.*
import cats.kernel.laws.discipline.*
import munit.DisciplineSuite
import java.util.Currency
import org.scalacheck.{Arbitrary, Cogen, Gen}
import scala.jdk.CollectionConverters.*

class JvmLawTests extends TestsConfig with DisciplineSuite {
implicit private val arbitraryCurrency: Arbitrary[Currency] = Arbitrary(
Gen.oneOf(Currency.getAvailableCurrencies().asScala)
)
implicit private val cogenCurrency: Cogen[Currency] = Cogen[String].contramap(_.getCurrencyCode())
Comment on lines +33 to +36
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.


checkAll("Eq[Currency]", EqTests[Currency].eqv)
checkAll("Hash[Currency]", HashTests[Currency].hash)
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ private[instances] trait AllInstancesBinCompat0 extends FiniteDurationInstances
private[instances] trait AllInstancesBinCompat1 extends SortedMapInstances with SortedSetInstances

private[instances] trait AllInstancesBinCompat2 extends DeadlineInstances

private[instances] trait AllInstancesBinCompat3 extends CurrencyInstances
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ private[instances] trait AllInstancesBinCompat0 extends FiniteDurationInstances
private[instances] trait AllInstancesBinCompat1 extends SortedMapInstances with SortedSetInstances

private[instances] trait AllInstancesBinCompat2 extends DeadlineInstances

private[instances] trait AllInstancesBinCompat3 extends CurrencyInstances
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package cats.kernel
package instances

import java.util.Currency

trait CurrencyInstances {
implicit def catsKernelStdHashForCurrency: Hash[Currency] = new CurrencyHash
massimosiani marked this conversation as resolved.
Show resolved Hide resolved
}

class CurrencyHash extends Hash[Currency] {
override def eqv(x: Currency, y: Currency): Boolean = x.getCurrencyCode().equals(y.getCurrencyCode())
override def hash(x: Currency): Int = x.getCurrencyCode().hashCode()
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ package object all
with AllInstancesBinCompat0
with AllInstancesBinCompat1
with AllInstancesBinCompat2
with AllInstancesBinCompat3
25 changes: 25 additions & 0 deletions kernel/src/main/scala/cats/kernel/instances/currency/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package cats.kernel
package instances

package object currency extends CurrencyInstances