scala cats aplicativo no se pudo compilar
Intento seguir https://typelevel.org/cats/typeclasses/applicative.html
trait Applicative[F[_]] extends Functor[F] {
def product[A, B](fa: F[A], fb: F[B]): F[(A, B)]
def pure[A](a: A): F[A]
}
// Implementación de ejemplo para Either con preferencia por la derecha
implicit def applicativeForEither[L]: Applicative[Either[L, *]] = new Applicative[Either[L, *]] {
def product[A, B](fa: Either[L, A], fb: Either[L, B]): Either[L, (A, B)] = (fa, fb) match {
case (Right(a), Right(b)) => Right((a, b))
case (Left(l), _) => Left(l)
case (_, Left(l)) => Left(l)
}
def pure[A](a: A): Either[L, A] = Right(a)
def map[A, B](fa: Either[L, A])(f: A => B): Either[L, B] = fa match {
case Right(a) => Right(f(a))
case Left(l) => Left(l)
}
}
No se logra compilar y genera el siguiente error:
not found: type *
implicit def applicativeForEither[L]: Applicative[Either[L, *]] = new Applicative[Either[L, *]] {
En cats utiliza ‘?’ en lugar de ‘*’ (por ejemplo, EitherTFunctor), pero también da error de compilación al copiar y pegar.
¿Qué debo hacer para solucionarlo?
davy.ai
El mensaje de error sugiere que hay un problema con la sintaxis de comodín
*
utilizada en la definición deapplicativeForEither
. Esta sintaxis es en realidad una característica del próximo lenguaje Scala 3 y todavía no es compatible con Scala 2, que es probablemente la versión que estás utilizando.Para solucionar esto, puedes reemplazar el comodín
*
por la sintaxis de?
que se utiliza en cats. Así, la definición deapplicativeForEither
quedaría de la siguiente manera:Con este cambio, el código debería compilar sin errores.