from scalawithcats book: value asRight is not a member of type parameter A

Viewed 90

I'm reading the "scala with cats" book. On page 339, I copied the example code but run with the 'value asRight is not a member of type parameter A' error.

My code is as follow, which is exactly the same with the book

import cats.Semigroup
import cats.instances.list._
import cats.syntax.either._
import cats.syntax.semigroup._

/**
 * This one is called implementation check and it is better
 *
 * @tparam E An template Error Type
 * @tparam A Template Data Type
 */
sealed trait Check[E, A] {
  import Check._

  def and(that: Check[E, A]): Check[E, A] =
    And(this, that)

  def apply(a: A)(implicit s: Semigroup[E]): Either[E, A] =
    this match {
      case Pure(f)          => f(a)
      case And(left, right) =>
        (left(a), right(a)) match {
          case (Right(a), Right(_)) => a.asRight
          case (Right(_), Left(e))  => e.asLeft
          case (Left(e), Right(_))  => e.asLeft
          case (Left(e1), Left(e2)) => (e1 |+| e2).asLeft
        }
    }
}

object Check {
  final case class And[E, A](left: Check[E, A], right: Check[E, A]) extends Check[E, A]

  final case class Pure[E, A](func: A => Either[E, A]) extends Check[E, A]

  def pure[E, A](f: A => Either[E, A]): Check[E, A] = Pure(f)

}

val a: Check[List[String], Int] =
  Check.pure {
    v =>
      if (v > 2) v.asRight
      else List ("Must be > 2").asLeft
  }

val b: Check[List[String], Int] =
  Check.pure {
    v =>
      if (v < - 2) v.asRight
      else List ("Must be < -2").asLeft
  }

val check: Check[List[String], Int] = a and b
check(5)
check(-10)

I put the above code in the script file 'check.sc' and my cat dependency is:

libraryDependencies += "org.typelevel" %% "cats-core" % "2.1.0"

When I use IDEA to run the snippet, it returns me

value asRight is not a member of type parameter A
case (Right(a), Right(_)) => a.asRight

Is that IDEA bug or something? Because if I paste the code into a scala file and run with a main function, it didn't complain anymore.

Below is correct:

package org.scala.ch10

import cats.Semigroup
import cats.instances.list._
import cats.syntax.either._
import cats.syntax.semigroup._

/**
 * This one is called implementation check and it is better
 *
 * @tparam E An template Error Type
 * @tparam A Template Data Type
 */
sealed trait CheckADT[E, A] {
  import CheckADT._

  def and(that: CheckADT[E, A]): CheckADT[E, A] =
    And(this, that)

  def apply(a: A)(implicit s: Semigroup[E]): Either[E, A] =
    this match {
      case Pure(f)          => f(a)
      case And(left, right) =>
        (left(a), right(a)) match {
          case (Right(a), Right(_)) => a.asRight
          case (Right(_), Left(e))  => e.asLeft
          case (Left(e), Right(_))  => e.asLeft
          case (Left(e1), Left(e2)) => (e1 |+| e2).asLeft
        }
    }
}

object CheckADT {
  final case class And[E, A](left: CheckADT[E, A], right: CheckADT[E, A]) extends CheckADT[E, A]

  final case class Pure[E, A](func: A => Either[E, A]) extends CheckADT[E, A]

  def pure[E, A](f: A => Either[E, A]): CheckADT[E, A] = Pure(f)

}

object Main {
  def main(args: Array[String]): Unit = {
    val a: CheckADT[List[String], Int] =
      CheckADT.pure {
        v =>
          if (v > 2) v.asRight
          else List ("Must be > 2").asLeft
      }

    val b: CheckADT[List[String], Int] =
      CheckADT.pure {
        v =>
          if (v < - 2) v.asRight
          else List ("Must be < -2").asLeft
      }

    val check: CheckADT[List[String], Int] = a and b
    println(check(5))
    println(check(-10))
  }
}

So what's wrong with my 'sc' file?

0 Answers
Related