2017-03-11 35 views
0

我想創建的Either實例中使用REPL例如asRight如何使用`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 
       ^

出了什麼問題上面的代碼?可以在REPL中使用asRight嗎?

回答

4

EitherIdOps其中包括asRightasLeft ops首次引入貓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) 
+0

非常感謝!你是對的。我正在使用較早的版本。 – Michael