I’m running into aproblem with type inference and I’m not sure if this is a known limitation or if I should file a bug.
In the snippet below, the type of `does_not_work`is inferred wrong.
Copy code
fun <IP, R> (() -> IP).then(f: (IP) -> R): () -> R = { f(this()) }
sealed class Option<out T>
data class Some<T>(val value: T) : Option<T>()
object None : Option<Nothing>()
fun maybeNull(): String? = "abcd"
fun <T> toOption(value: T?) = if (value != null) {
Some(value)
} else {
None
}
fun toStringOption(value: String?) = value?.let { Some(value) } ?: None
//should be inferred to () -> Option<String>, but is inferred to () -> Option<String?>
val does_not_work: () -> Option<String> = ::maybeNull.then(::toOption)
//type is correct if no generic type is used
val works: () -> Option<String> = ::maybeNull.then(::toStringOption)
d
dmitriy.novozhilov
12/29/2019, 11:36 AM
it inferes fine with enabled new inference algorithm
you can enable it with
-Xnew-inference
compiler flag
r
raulraja
12/29/2019, 11:37 AM
@dmitriy.novozhilov Is the new inference going to be the default in 1.4? thanks.