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

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
QuokMoon
  • 4,387
  • 4
  • 26
  • 50

1 Answers1

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.

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
  • I am just wondering to remove Any from ResultMapper class like : `ResultMapper ` cause many inbuilt class not allow with Any explicit type. Do you think this one is ok ? – QuokMoon Feb 22 '18 at 08:05
  • That’s an alternative, sure you can do it when nullable types are okay for your use case – s1m0nw1 Feb 22 '18 at 08:06