不满意:推断类型 T?不是 Any 的子类型

Not satisfied: inferred type T? is not subtype of Any

为了映射结果,我实施了密封通用 class :

public sealed class ResultMapper<out T : Any> {
    public class Ok<out T : Any>(public val value: T,
                            override val response: Response) : Result<T>(), ResponseResult {
        override fun toString(): String = "Result.Ok{value=$value, response=$response}"
    }
}

   public interface ResponseResult {
     val response: Response
   } 

现在我想这个 class 应该按预期的方式工作:

 ResultMapper.Ok(body,raw)

private class Body<T>() {
  onResponse(response: Response, raw: Raw) {
   ResultMapper.Ok(response.body(),response.raw()) --> It returned an exception
  }
}

Constructor Ok is not satisfied: inferred type T? is not subtype of Any

class Body 有一个泛型类型参数 T 没有任何界限,即它就像定义 T: Any?Ok 的类型参数是限于T: Any。您应该调整 Body 以不允许可空类型:

class Body<T: Any>

或者,删除其他 classes 中的上限。