softshipper
softshipper

Reputation: 34081

Type mismatch with generics sum type

I have a sum trait, that looks like as follow:

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]

When I try to create a new variable as:

val s1: Sum[Int, Nothing] = Success(4)

I've got following error:

Error:(5, 41) type mismatch;
 found   : Int(4)
 required: Nothing
    val s1: Sum[Int, Nothing] = Success(4)

Why?

And why this is working:

val s1: Sum[Int, Int] = Success(4)

Upvotes: 1

Views: 45

Answers (1)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149538

Because B is the second type parameter, not the first:

val s1: Sum[Nothing, Int] = Success(4)    

Upvotes: 4

Related Questions