Skip to content

Instantly share code, notes, and snippets.

@lazyval
Created November 27, 2018 15:37
Show Gist options
  • Save lazyval/d8fb8ef009576dc9133c8640cf6936e0 to your computer and use it in GitHub Desktop.
Save lazyval/d8fb8ef009576dc9133c8640cf6936e0 to your computer and use it in GitHub Desktop.
RecordMatcher failure example
libraryDependencies += "me.lyh" % "shapeless-datatype-core_2.12" % "0.1.10"
import java.math.MathContext
import shapeless.datatype.record._
object Example extends App {
case class BigBoy(x: BigDecimal)
implicit def compareBigDecimals(x: BigDecimal, y: BigDecimal): Boolean = {
println("custom bigdecimal comparison invoked")
x.compareTo(y) == 0
}
val x = BigDecimal("103040.20303", MathContext.DECIMAL64)
val y = BigDecimal("103040.20303")
println(s"Explicit equals comparison: ${x.equals(y)}") // gives true
println(s"Explicit invokation of compare X vs Y: ${compareBigDecimals(x, y)}") // gives true
println(s"Matcher comparison X vs Y: ${RecordMatcher[BigBoy](BigBoy(x), BigBoy(y))}") // gives false
}
kostyag-mbp:~/projects/bigboy$ sbt run
[info] Loading settings for project global-plugins from idea.sbt,plugins.sbt ...
[info] Loading global plugins from /Users/kostyag/.sbt/1.0/plugins
[info] Loading project definition from /Users/kostyag/projects/bigboy/project
[info] Loading settings for project bigboy from build.sbt ...
[info] Set current project to bigboy (in build file:/Users/kostyag/projects/bigboy/)
[info] Compiling 1 Scala source to /Users/kostyag/projects/bigboy/target/scala-2.12/classes ...
[info] Done compiling.
[info] Packaging /Users/kostyag/projects/bigboy/target/scala-2.12/bigboy_2.12-0.1.0-SNAPSHOT.jar ...
[info] Done packaging.
[info] Running Example
Explicit equals comparison: true
custom bigdecimal comparison invoked
Explicit invokation of compare X vs Y: true
Matcher comparison X vs Y: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment