Kotlin and Upper type estimates with covariance

Does Kotlin support upper and lower type constraints in setting covariance. For example, I want to say

class Foo<out T> { fun or<U of T or greater>(other: U): <U> = ... } 

which in Scala would be

 class Foo[+T] { def or[U >: T](other: U): U = ... } 

But the compiler does not seem to like this, he complains about the covariance of a parameter of type T.

+5
source share
1 answer

Kotlin does not support lower ratings at this point. Sometimes you can get away with defining an extension function instead of a member:

 fun <T> Foo<T>.or(other: T): T = ... 
+5
source

Source: https://habr.com/ru/post/1212394/


All Articles