喂! :)
我不愿意一直跑到stackoverflow论坛来获得一些帮助,以帮助我解决另一个陌生的scala / cats问题。 问题是:似乎没有真正有用的文档,只有一些毫无价值的-至少对我来说-复制行。
能否请您指出一些有用的文档?一些 real 代码?不只是REPL中的行?
在这里,我只是尝试使用scala / cats Eq和Show类型类... 我到底在做什么错了?
课程:
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!
}
}
所以,请问有没有 毫无价值的文档?
预先感谢
有比我更好的一天
亚历克斯
答案 0 :(得分:3)
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中每个对象可用的方法,无法覆盖它。
2。。我建议您阅读Welsh, Gurnell "Scala with Cats"。 Scaladoc也很好,但是除非阅读cats
库中有关包和隐式组织的介绍性章节,否则您无法有效地导航它。