2016-11-17 129 views
1

我有以下代码,我从这里得到:http://underscore.io/blog/posts/2015/06/10/an-introduction-to-cats.html猫无法解析符号| @ |

import cats.data.Xor 
import cats.data.{Validated, Xor} 
import cats.syntax.apply._ // For |@| syntax 
import cats.std.list._ 
val v1: ValidatedR = valid(1) 
val v2: ValidatedR = invalid(List("Accumulates this")) 
val v3: ValidatedR = invalid(List("And this")) 
(v1 |@| v2 |@| v3) map { _ + _ + _ } 

但是,我越来越:

Cannot resolve symbol |@| 

build.sbt

val snapshots = "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots" 

val algebraVersion = "0.2.0-SNAPSHOT" 
val catsVersion = "0.1.0-SNAPSHOT" 

val algebra = "org.spire-math" %% "algebra" % algebraVersion 
val algebraStd = "org.spire-math" %% "algebra-std" % algebraVersion 

val cats  = "org.spire-math" %% "cats-core" % catsVersion 
val catsStd = "org.spire-math" %% "cats-std" % catsVersion 

scalaVersion := "2.11.6" 

libraryDependencies ++= 
    Seq(
    algebra, algebraStd, 
    cats, catsStd 
) 

resolvers += snapshots 

还有什么,我应该是进口或者使用?

+0

导入现在是'cats.syntax.cartesian._','List'实例现在可以通过'cats.instances.list._'导入。可能最简单的方法是使用'import cats.implicits._'。 –

+0

当导入'cats.syntax.cartesian._'时,我得到'错误:(2,22)对象笛卡儿不是包cats.syntax'的成员 ^ – octavian

+0

您是否正在使用文章中提到的猫版本? –

回答

5

这个例子有点过时了。从那时起,一些事情发生了改变:

  • |@|现在由Cartesian型类提供相比前作Apply类型的类。
  • 来自Scala标准库的Option,List等类型的输入已从cats.std.xxx更名为cats.instances.xxx
  • 最新版本的Cats不再具有Xor数据类型,而是使用scala.util.Either数据类型。

就像我在我的评论中提到的那样,使用“超级”导入cats.implicits._更容易。

对于一些相似(和最新)的示例,您可以查看ValidatedEither的Cats文档。

+1

请注意:批量导入implicits更容易,但可以使编译更加昂贵。精确输入implicits通常是可取的。 –

+0

你有一点@GabrielePetronella,但你不认为他们是一个更先进的,而不是初学者友好(导致这样的问题)? Cats文档也尝试在任何地方使用“超级”导入,请参见[#1026](https://github.com/typelevel/cats/issues/1026)。 –

+0

是的,我同意。我的观点是:超级进口的成本可能并不明显,因此可能值得一提m –