![]() ![]() The compiler supports several flavors of nullability annotations, including: Java types that have nullability annotations are represented not as platform types, but as actual nullable or non-null Kotlin types. (Mutable)Collection! means "Java collection of T may be mutable or not, may be nullable or not",Īrray! means "Java array of T (or a subtype of T), nullable or not" Nevertheless, the compiler and IDE need to display them sometimes (for example, in error messages or parameter info), so there is a mnemonic notation for them: Notation for platform typesĪs mentioned above, platform types can't be mentioned explicitly in the program, so there's no syntax for them in the language. Overall, the compiler does its best to prevent nulls from propagating far through the program although sometimes this is impossible to eliminate entirely, because of generics. Assertions are also emitted when you pass platform values to Kotlin functions expecting non-null values and in other cases. This prevents Kotlin's non-null variables from holding nulls. If you choose a non-null type, the compiler will emit an assertion upon assignment. ![]() Val notNull: String = item // allowed, may fail at runtime Val nullable: String? = item // allowed, always works
0 Comments
Leave a Reply. |