简体   繁体   English

Scala中的Cats之外是否有任何三元等于===方法?

[英]Are there any triple equals === methods outside of Cats in Scala?

I have spent a while searching on Google for a non-Cats triple equals method, but can't find anything apart from Scalaz. 我花了一段时间在Google上搜索了非猫三重等于方法,但除了Scalaz找不到任何东西。 Unfortunately, I have been unable to work out the import for === in this library. 不幸的是,我无法在此库中计算===的导入。

Can someone help, many thanks. 有人可以帮忙,非常感谢。

If all you need is === , you could very easily mimic the behaviour from Cats with your own function: 如果您只需要=== ,则可以使用自己的函数轻松模拟Cats的行为:

implicit class AnyWithTripleEquals[T](a: T) {
  def ===(b: T): Boolean = a equals b
}

/*
scala> "2" === "3"
res0: Boolean = false

scala> "2" === 3
<console>:13: error: type mismatch;
 found   : Int(3)
 required: String
       "2" === 3
*/

Another library that provides === is scalactic , which is basically a set of utilities used by ScalaTest, which are packaged as a separate lib. 提供===另一个库是scalactic ,它基本上是ScalaTest使用的一组实用程序,这些实用程序打包为单独的库。

import org.scalactic._
import TypeCheckedTripleEquals._

"Hello" === "Hello" //true
1 === "Hello" //won't compile

you can also "configure" how your equality is being resolved with implicits: 您还可以“配置”如何使用隐式方法解决您的平等问题:

import org.scalactic._
import TripleEquals._
import StringNormalizations._
import Explicitly._

implicit val strEquality = decided by defaultEquality[String] afterBeing lowerCased

"Hello" === "hello"                           // true
"normalized" === "NORMALIZED"                 // true

Of the top of my head other libraries that use === are eg: 在我的头上,其他使用===库例如:

but it isn't the same use case as in Cats/Scalaz. 但这与Cats / Scalaz中的用例不同。

If you want to use it in Cats you need: 如果要在Cats中使用它,则需要:

  • syntax - import cats.syntax.eq._ or import cats.syntax.all._ or import cats.implicits._ (if you duplicate import of syntax, Scala won't be able to resove it) 语法- import cats.syntax.eq._import cats.syntax.all._import cats.implicits._ (如果重复导入语法,Scala将无法解决它)
  • instance - if you compare 2 A you need an implicit instance of cats.Eq[A] . instance-如果比较2 A ,则需要cats.Eq[A]的隐式实例。 instances for List s, Map s etc. can be found in cats.instances.list._ , cats.instances.map._ , cats.instances.all._ or cats.implicits._ (same rule as above). ListMap等等的实例可以在cats.instances.list._cats.instances.map._cats.instances.all._cats.implicits._ (与上述规则相同)。 There should be instances for all "normal" types but if you have your own, you need to either provide Eq instance on your own or derive it with something like Kittens . 所有“普通”类型都应该有实例,但是如果您有自己的实例,则需要自己提供Eq实例,或者使用Kittens之类派生它。

If you are missing some implicit (or if some implicit is ambiguous, because you imported the same things from 2 different places) the syntax won't work. 如果您缺少一些隐式(或者某些隐式是模棱两可的,因为您从2个不同的地方导入了相同的内容),则该语法将不起作用。

Same thing about Scalaz though imports and type classes will have other names. 关于Scalaz的事情也一样,尽管导入和类型类将具有其他名称。

If you don't mind some performance penalty (caused by isInstanceOf inside equals ) and lack of flexibility regarding the definition of equality check, you can use @sachav's solution. 如果您不介意性能下降(由isInstanceOf内部equals引起)并且缺乏关于相等性检查定义的灵活性,则可以使用@sachav的解决方案。

Regarding scalaz try 关于scalaz尝试

import scalaz._
import Scalaz._

42 === "hello" // error: type mismatch; found: String("hello") required: Int

where 哪里

libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.2.28"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM