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

1

5

To map the result I have implemented sealed generic 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
   } 

Now I suppose to this class should work as expected below behaviour:

 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

Fatimafatimah answered 22/2, 2018 at 7:4 Comment(0)
W
8

The class Body has a generic type parameter T without any bounds, i.e. it’s like defining T: Any? whereas Ok’s type parameter is confined to be T: Any. You should adjust Body to not allow nullable types either:

class Body<T: Any>

Alternatively, remove the upper bound in the other classes.

Wonacott answered 22/2, 2018 at 7:9 Comment(2)
I am just wondering to remove Any from ResultMapper class like : ResultMapper <out T> cause many inbuilt class not allow with Any explicit type. Do you think this one is ok ?Fatimafatimah
That’s an alternative, sure you can do it when nullable types are okay for your use caseWonacott

© 2022 - 2024 — McMap. All rights reserved.