Daniele Segato
05/22/2025, 3:36 PM?
and !!
will be used as well for error types, how are we going to differentiate between null and error in a function returning a nullable type?
fun foo() String? | SomeError
I was surprised this wasn’t addressed at all in the talk.
Are we supposed to either write functions with error union types OR functions returning nullable without error union types?
What about generics versions of this?
A null
can be a valid successful responsemikhail.zarechenskiy
05/22/2025, 8:55 PMString?
can be viewed as String | Null
, which has a shorthand syntax. The generalized rules of ?.
and !!
apply here as well:
foo()?.length // type is Int | Null | SomeError ~ Int? | SomeError
foo()!! // string -> NOP, null -> NPE, SomeError -> KotlinException
So the behavior is the same as if the return type was just `String?`: a safe call results in Int?
, and !!
throws an NPE if the value is null
. In the presence of errors, we extend this behavior.
AThat’s correct, this doesn't change. However, if you want to explicitly signal that a value might be missing, it’s better to consider returning something likecan be a valid successful responsenull
String | NotFound | SomeError
Daniele Segato
05/28/2025, 6:55 PMmikhail.zarechenskiy
06/01/2025, 7:31 PMcan we expect existing APIs to do the same?
Such as collection.first() returning a NoSuchElement and firstOrNull() getting deprecated?
What will happen to the current APIs with this change?Most likely, the existing API will stay the same. Especially functions like
first
that are everywhere. We don't plan to have any massive migration with the introduction of this feature. Speaking of contracts — we'll see, but what is your use-case regarding contracts?Daniele Segato
06/03/2025, 8:39 AM