// a Java interface:
// public interface Store<T> { T save(T value); }
// *** before: ***
// when overriding a Java generic there was no way to say
// "T is never null here", so the platform type leaked through
fun <T> orDefaultOld(value: T?, def: T): T = value ?: def
// *** in version 1.7: ***
fun <T> orDefault(value: T?, def: T & Any): T & Any = value ?: def
class Box<T> {
private var stored: T? = null
fun put(value: T & Any) { // not nullable even if T itself is
stored = value
}
}
fun main() {
println(orDefault(null, "fallback"))
Box<String?>().put("value")
// Box<String?>().put(null) // <- Error, which is the whole point
}