Can I write a contract that specifies the non-null...
# getting-started
c
Can I write a contract that specifies the non-nullability of a returned field?
Copy code
class Foo(
    val a: Int?,
    val b: Int?,
)

fun foo(): Foo {
    // …
}
If
foo
returns (no exception), then
Foo.a
is guaranteed non-null
m
I think that’s the same request as from https://kotlinlang.slack.com/archives/C0B9K7EP2/p1790952423256719?thread_ts=1790949083.428709&cid=C0B9K7EP2 And the answer is “no” atm
y
Unfortunately no, but you can use type parameters and such to do tricks:
Copy code
class FooTout A: Int?, out B: Int?(
    val a: A,
    val b: B,
)
typealias Foo = FooT*, *
fun foo(): FooTInt, * {
    // ..
}
This works well with contracts, too! The intuition here to move “value-level information” (i.e. which properties have which types) into type-level information (using type parameters).