阿罗哈! :)
我不高兴一直在运行stackoverflow论坛,以获得scala / cats向我抛出的另一个奇怪的帮助。问题是:似乎没有真正有用的文档,只有一些毫无价值 - 至少对我来说 - 重复行。
可以请你们中的某些人指出一些有用的文件吗?一些真正的代码?不只是repl中的行?
在这里,我只是尝试使用scala / cats Eq和Show typeclasses ...我到底做错了什么?
班级:
package org.hudelundpfusch.utilites.decisions.data
import cats.Show
import cats.kernel.Eq
case class Fact[+T <: Any](name: String, value: T)
extends Equals {
override def canEqual(that: Any): Boolean = that match {
case _: Fact[_] => true
case _ => false
}
override def equals(other: Any): Boolean = other match {
case that: Fact[_] =>
(that canEqual this) &&
name == that.name &&
value == that.value
case _ => false
}
override def hashCode(): Int = {
val state = Seq(name, value)
state.map(_.hashCode()).foldLeft(0)((a, b) => 31 * a + b)
}
override def toString = s"Fact(name=$name, value=$value)"
}
case object Fact {
implicit val factEq: Eq[Fact[_]] = Eq.fromUniversalEquals[Fact[_]] // Neither of this works
// implicit def factEq: Eq[Fact[_]] = new Eq[Fact[_]] {
// def eqv(x: Fact[_], y: Fact[_]): Boolean = (x != null, y != null) match {
// case (true, _) => x.equals(y)
// case (_, true) => y.equals(x)
// case _ => true
// }
// }
implicit def factShow[T]: Show[Fact[T]] = (t: Fact[T]) => t.toString // Example calls for 'implicit val factShow[Fact[_]]' but that doesn't work
}
而最大的惊喜:
package org.hudelundpfusch.utilites.decisions
import cats._
import cats.data._
import cats.syntax._
import cats.implicits._
import cats.implicits.eq
import com.typesafe.scalalogging.LazyLogging
import org.hudelundpfusch.utilites.decisions.data.Fact
import org.hudelundpfusch.utilites.decisions.data.Fact._
// Tried to import everything that came to my mind to make the stuff working
object Fuddel
extends App
with LazyLogging {
logger.info("Let's start to fuddel!")
this.fuddel()
logger.info("Enough with fuddling!")
def fuddel(): Unit = {
val fact1: Fact[String] = Fact[String]("FactName", "FactValue")
println(s"${fact1.show}")
val fact2: Fact[String] = Fact[String]("FactName", "FactValue")
println(s"${fact2.show}")
println(s"${fact1.equals(fact2)}")
println(s"${fact1 == fact2}")
// println(s"${fact1 === fact2}") // Not resolved...According to the repl example this should work with implicits imported
println(s"${fact1 eq fact2}") // False? Oh joy! Thanks to the great repl example!
}
}
那么请问,有没有任何文件没有价值?
提前致谢
有一个比我更美好的一天
亚历克斯
1.这里编译得很好(我删除了你的包名和日志依赖):
import cats.Show
import cats.kernel.Eq
case class Fact[+T](name: String, value: T) extends Equals {
override def canEqual(that: Any): Boolean = that match {
case _: Fact[_] => true
case _ => false
}
override def equals(other: Any): Boolean = other match {
case that: Fact[_] => true // TODO: replaced, irrelevant
case _ => false
}
override def hashCode(): Int = {
val state = Seq(name, value)
state.map(_.hashCode()).foldLeft(0)((a, b) => 31 * a + b)
}
override def toString = s"Fact(name=$name, value=$value)"
}
case object Fact {
implicit def factEq[A]: Eq[Fact[A]] = Eq.fromUniversalEquals[Fact[A]]
implicit def factShow[T]: Show[Fact[T]] = (t: Fact[T]) => t.toString
}
请注意factEq[A]
中的通用量化而不是通配符。然后在Fuddel.scala
:
import cats.syntax.show._
import cats.syntax.eq._
import Fact._
object Fuddel
extends App {
this.fuddel()
def fuddel(): Unit = {
val fact1: Fact[String] = Fact[String]("FactName", "FactValue")
println(s"${fact1.show}")
val fact2: Fact[String] = Fact[String]("FactName", "FactValue")
println(s"${fact2.show}")
println(s"${fact1.equals(fact2)}")
println(s"${fact1 == fact2}")
println(s"${fact1 === fact2}")
println(s"${fact1 eq fact2}")// must be false, different instances
}
}
请注意,eq
是Scala中每个对象都可用的方法,无法覆盖它。
我建议你阅读Welsh, Gurnell "Scala with Cats"。 Scaladoc也很好,但是在你阅读关于cats
库中的包和implicits组织的介绍性章节之前,你无法有效地进行导航。