-
Notifications
You must be signed in to change notification settings - Fork 0
Kotlin Notes
Li-Wei Yap edited this page May 24, 2021
·
3 revisions
- At compile time,
kotlin.Anyandjava.lang.Objectare treated as two different types. - At run time,
kotlin.Any!andjava.lang.Objectare treated as the same type. But the!also means that the type is a platform type, which has implications with respect to disabling null checks, etc.- Any reference in Java may be
null, which makes Kotlin’s requirements of strict null-safety impractical for objects coming from Java. Types of Java declarations are treated specially in Kotlin and called platform types. Null-checks are relaxed for such types, so that safety guarantees for them are the same as in Java.- Specially treated types are not loaded from Java "as is", but are mapped to corresponding Kotlin types. The mapping matters only at compile time; the run-time representation remains unchanged. Java’s primitive types are mapped to corresponding Kotlin types (keeping platform types in mind).
- When we call methods on variables of platform types, Kotlin does not issue nullability errors at compile time, but the call may fail at run time, because of a null-pointer exception or an assertion that Kotlin generates to prevent nulls from propagating.
- Any reference in Java may be
Whenever you have a lambda function with exactly one parameter, you don’t have to declare the parameter explicitly; you can just use the name it and omit ->. This is because the compiler can figure the signature out itself. The parameter will be implicitly declared under it. This makes a lot of the language constructs like map, filter, etc. easier to use as you do not have to specify the name of the parameter (you can if you want to).
Lambda expressions and anonymous functions are function literals. Function literals are functions that are not declared but passed immediately as an expression.