简体   繁体   中英

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)

因为B是第二个类型参数,而不是第一个:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM