softshipper
softshipper

Reputation: 34087

Cannot resolve symbol A

I have following trait:

sealed trait Sum[+A, +B]

final case class Failure[A](value: A) extends Sum[A, Nothing]

final case class Success[B](value: B) extends Sum[Nothing, B]

object Sum {

  def flatMap[AA >: A, B, C](s: Sum[AA, B], f: B => Sum[AA, C]): Sum[AA, C] =
    s match {
      case Failure(v) => Failure(v)
      case Success(v) => f(v)
    }

  def fold[A, B, C](s: Sum[A, B], success: A => C, failure: B => C): C =
    s match {
      case Failure(v) => failure(v)
      case Success(v) => success(v)
    }

  def map[A, B, C](s: Sum[A, B], success: A => C): Sum[A,C] =
    fold(s, succ => Success(success(succ)), fail => Failure(fail))

}

and the compiler complain:

Cannot resolve symbol A

by:

flatMap[AA >: A, B, C]

What am I doing wrong?

Upvotes: 1

Views: 501

Answers (1)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149558

What am I doing wrong?

You're defining a lower bound for a type parameter that doesn't exist. What is A in this context? You're telling the compiler "I want AA to have a lower bound of type A", but the compiler doesn't know a generic type parameter A because it wasn't declared.

If you want to have two type parameter, where one is a lower bound on another (or more generally any bound), it needs to be declared first:

def flatMap[A, AA >: A, B, C]

Upvotes: 4

Related Questions