我定义了这些类。
trait ResultTrait {
}
case class PostResult (
@Key("_id") id: String,
success: String,
errors: Seq[String] = Seq.empty
) extends ResultTrait
case class PostError (
message: String,
errorCode: String
) extends ResultTrait
这不会编译。它给出错误"必需的T,但找到PostResult(或PostError)"。
def postLead[T <: SFDCResult](accessToken: AccessToken):
Future[T] = {
// depends on response from request, return PostResult or PostError
}
正如@Travis Brown已经说过的,看起来你试图通过泛型来表达返回类型的可变性(即"它要么是PostResult
要么是PostError
"),而实际上你所需要的只是父特征。
假设您的SDFCResult
是匿名错误,您打算使用ResultTrait
,我将使用以下:
// Make the trait sealed so we can only have our two known implementations:
sealed trait ResultTrait {}
...
// Two subclasses as before
然后你的方法应该是:
def postLead(accessToken: AccessToken):Future[ResultTrait] = {
// depends on response from request, return PostResult or PostError
}