Curyous
Curyous

Reputation: 8866

What does ! mean on a Kotlin return value?

I'm trying to use a Task result that has a type of Location!. What does that mean for how I am supposed to handle nullability? Could it be null or not?

I couldn't find this kind of declaration in the Null Safety section of kotlinlang.org.

Upvotes: 2

Views: 102

Answers (1)

s1m0nw1
s1m0nw1

Reputation: 81859

A Type notated with ! is called platform type, which is a type coming from Java and thus can most probably be null. It’s what the Kotlin compiler infers by default when calling Java (for the most basic cases, Java methods can be annotated to get around this). You should handle platform types as nullable types, unless you certainly know that the particular API will never return null. The compiler allows platform types to be assigned to variables of both nullable and non-null types.

Notation for Platform Types

[...]

T! means "T or T?" [...]

You could refer to platform types as "types of unknown nullability". Also important to know is that you cannot use the exclamation-marked type for your own types, it's not part of the Kotlin syntax, it's only a notation.

Upvotes: 7

Related Questions