Sometimes, though, you know better than the compiler. More importantly, just because if (numberizer() != null) succeeded and we went into the if block, the compiler has no guarantee that some future numberizer() call will return a non- null value, so it gives us a compile error for numberizer().rem(2), demanding a safe call there. It looks at the return type, sees that it is Int?, and assumes that it could be null. Kotlin’s compiler does not attempt to examine the implementation of numberizer(). That function is declared to return an Int?, even though its implementation happens to always return 1. ![]() ![]() Here, we get our number from a numberizer() function. Fun numberizer (): Int ? = 1 fun evenOrOddOrNull () evenOrOddOrNull ()
0 Comments
Leave a Reply. |