如何使用`asRight`创建与猫的任何一个实例

Mic*_*ael 2 scala either scala-cats scala-repl

我想在REPL中创建一个Either使用实例asRight:

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
                 ^
Run Code Online (Sandbox Code Playgroud)

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

Mic*_*jac 6

EitherIdOps其中包括asRightasLeftops最初是在cat 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)
Run Code Online (Sandbox Code Playgroud)