scala - 如何使用 `asRight` 创建带有猫的 Either 实例

标签 scala either scala-cats scala-repl

我正在尝试在 REPL 中使用 asRight 创建 Either 的实例:

import cats._
import cats.data._
import cats.implicits._ 

scala> val x = "xxx".asRight
<console>:20: error: value asRight is not a member of String
       val x = "xxx".asRight
                 ^

scala> import cats.syntax.either._
import cats.syntax.either._

scala> val x = "xxx".asRight
<console>:23: error: value asRight is not a member of String
       val x = "xxx".asRight
                 ^

上面的代码有什么问题?是否可以在 REPL 中使用 asRight

最佳答案

EitherIdOps 包括 asRightasLeft ops 首次在 cats 0.9.0(撰写本文时的最新版本)中引入。您很可能使用的是早期版本。

scala> import cats._, implicits._
import cats._
import implicits._

scala> "xxx".asRight
res0: Either[Nothing,String] = Right(xxx)

scala> "xxx".asRight[Int]
res1: Either[Int,String] = Right(xxx)

关于scala - 如何使用 `asRight` 创建带有猫的 Either 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42737887/

相关文章:

scala - 将 StructType 定义为函数 Spark-Scala 2.11 的输入数据类型

scala - 要么,选项和理解

scala - 获取任何一个的值(value)

scala - 如何使用 Monoid Scala?

scala - `F[Something[F]]` 背后的意图是什么?

scala - 在 Scala 中通过索引设置 ArrayBuffer 中的值

scala - 如何加入数据框(来自数据集的集合)?

unit-testing - 如何从 Dart (Dartz) 中的任一种类型中轻松提取左或右

functional-programming - F# 中是否有类似于 scalaz、cats 和 arrow (Kotlin) 的库?

scala - 在intellij中调试scala代码?