Created
May 26, 2020 12:34
-
-
Save makiftutuncu/16557c27aa094f5361b5780c4de9675f to your computer and use it in GitHub Desktop.
Trick to Use Covariant (out) Type in Contravariant (in) Position in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class Option<out A>(open val value: A?) { | |
fun <B> fold(ifNone: () -> B, ifSome: (A) -> B): B = | |
when (this) { | |
is None -> ifNone() | |
is Some -> ifSome(this.value) | |
} | |
// This doesn't compile because `out` type A is in `in` position | |
fun getOrElseThatWontWork(default: () -> A): A = | |
when (this) { | |
is None -> default() | |
is Some -> this.value | |
} | |
} | |
data class None<A>: Option<A>(null) | |
data class Some<A>(override val value: A): Option<A>(value) | |
// But this works even though A is in `in` position here! | |
// | |
// This is an inlined extension function with a reified type A with no variance restrictions on its definition. | |
// So we are free to use A in `in` position. | |
inline fun <reified A> Option<A>.getOrElse(crossinline default: () -> A): A = fold({ default() }, { a -> a }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment