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

时间:2018-02-22 07:04:49

标签: generics kotlin bounds kotlin-android-extensions

为了映射结果,我实现了密封的泛型类:

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
   } 

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

 ResultMapper.Ok(body,raw)

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

构造函数Ok不满意:推断类型T?不是的子类型   的不限

1 个答案:

答案 0 :(得分:4)

<plugin> <artifactId>maven-source-plugin</artifactId> <executions> <execution> <id>source-jar</id> <goals> <goal>jar</goal> </goals> <phase>prepare-package</phase> <configuration> <outputDirectory>${project.build.outputDirectory}</outputDirectory> <finalName>filename-of-generated-jar-file</finalName> <attach>false</attach> </configuration> </execution> </executions> </plugin> 有一个没有任何边界的泛型类型参数Body,即它就像定义TT: Any?的类型参数被限制为{{1} }}。您应该将Ok调整为不允许可以为空的类型:

T: Any

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