简体   繁体   English

类型与泛型求和类型不匹配

[英]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)    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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