Is there a way to declare a definitely non-null pr...
# getting-started
s
Is there a way to declare a definitely non-null property by a delegate? So that such an expression would be impossible
val property: Int? by delegate(…)
but this should be ok
val property: Int by delegate(…)
.
Copy code
data class Holder<T : Any>(val value: T)

fun <T : Any> delegate(holder: Holder<T>) =
    ReadOnlyProperty<Any, T> { _, _ -> holder.value }

class Foo {
    // ERROR: Type argument is not within its bounds: should be subtype of 'kotlin. Any'.
    val holder = Holder<Int?>(1)

    // why OK?
    val property: Int? by delegate(Holder(1))
}
y
Because it's a
val
property, the only check that kotlin does is to make sure that the property type is a supertype of the delegate's
getValue
type. In other words, this works for the same reason that:
Copy code
val property: Int? = Holder(1).value
works.
s
👍 or
val property: Int? = 1
...
s
Thank you! Well, ok… It seems that above examples are correct and expected. But is it only me, or that doesn’t seem quite logical when such a restriction
T : Any
appears for the generic type? In my case a holder throws a runtime exception if the value is null, so it would return only non-nullable values. If I add a double-bang operator like this
{ _, _ -> holder.value!! }
there is a warning “Unnecessary non-null assertion (!!) on a non-null receiver of type ‘T’.” So I expect that compiler assumes that a nullable type
Int?
with such a restriction is not a valid case.
s
If you wrote out all the types in full, it'd be
Copy code
val property: Int? by delegate<Int>(Holder<Int>(1))
You're certainly correct that
delegate<Int?>(…)
would not be valid. But
delegate<Int>(…)
is always going to be a valid delegate for any supertype of
Int
, including
Int?
e
if you think about how it desugars, it's basically
Copy code
private val propertyDelegate = delegate(Holder(1))
val property: Int?
    get() = propertyDelegate.getValue()
which is not an error. the IDE does warn about that
?
in similar circumstances though, so perhaps file a feature request in YouTrack
s
@Sam @ephemient thank you very much!