Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin and Upper Type Bounds with Covariance

Tags:

kotlin

Does Kotlin support upper / lower type bounds in a covariance setting. Eg, 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 doesn't seem to like this, it complains about the covarianceness of the type parameter T.

like image 587
sksamuel Avatar asked Feb 01 '15 20:02

sksamuel


1 Answers

Kotlin does not support lower bounds 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 = ...
like image 139
Andrey Breslav Avatar answered Oct 19 '22 16:10

Andrey Breslav