【问题标题】:How to use `asRight` to create Either instance with cats如何使用`asRight`创建带有猫的Either实例
【发布时间】:2017-03-12 02:58:20
【问题描述】:

我正在尝试在 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

【问题讨论】:

    标签: scala either scala-cats scala-repl


    【解决方案1】:

    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)
    

    【讨论】:

    • 非常感谢!你说的对。我使用的是早期版本。
    猜你喜欢
    • 2020-03-05
    • 2018-06-23
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    相关资源
    最近更新 更多