scalaeitherscala-catsscala-repl

How to use `asRight` to create Either instance with cats


I am trying to create an instance of Either using asRight in REPL:

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
                 ^

What's wrong with the code above ? Is it possible to use asRight in REPL ?


Solution

  • EitherIdOps which includes asRight and asLeft ops was first introduced in cats 0.9.0 (the latest release at the time of writing). You are most likely using an earlier version.

    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)