Kotlin Notes - liweiyap/pomodoro-made-easy-on-android GitHub Wiki
Kotlin Notes
Any vs Object
Source(https://stackoverflow.com/questions/38761021/does-any-object)
- 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
Casting Any to Int
Source(https://stackoverflow.com/questions/43876910/how-to-convert-any-to-int-in-kotlin)
Passing interface argument as a lambda function or otherwise
Source(https://stackoverflow.com/questions/41649535/pass-interface-as-parameter-in-kotlin)
The it keyword: the implicit name of a single parameter in a lambda function
Source(https://kotlinlang.org/docs/lambdas.html#it-implicit-name-of-a-single-parameter) Source(https://discuss.kotlinlang.org/t/it-keyword/6869/2)
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).
Function literals
Source(https://kotlinlang.org/docs/lambdas.html#lambda-expressions-and-anonymous-functions)
Lambda expressions and anonymous functions are function literals. Function literals are functions that are not declared but passed immediately as an expression.