Friday, May 1, 2020

Kotlin vs Java continued...
Kotlin code is multiplatform code and yet it allows common code to depend on platform specific invocations. Usually, a set of interfaces is defined in the common code but and they are implemented in platform specific modules. Kotlin does away with this approach because it is not ideal. Interfaces is rather limiting and is not the only approach
Instead Kotlin provides expected and actual declarations. A common module can define expected declarations and the platform module can provide actual declarations. The actual declarations correspond to the expected ones.
The class declarations in these modules bear the keywords expect and actual. The compiler ensures that all platform modules have implementations corresponding to the expected declarations. This works with generics and type alias also.
Kotlin provides features for introspecting the structure of our own program at runtime. It makes functions and properties first class citizens in the language so that their type or name can
be looked up.
The runtime reference to a Kotlin class is one of the best examples of reflection. This reference is a type and is different from the Java reference. The Kotlin class corresponding to a Java class can be obtained with the   .Kotlin property.
Since the reference is a type, it allows us to use them for instantiation.  There are several callable references that have a common supertype.
Function references can be called directly. They can also be called with their reference type using the ‘::’ notation.
The method reference can also be stored in a variable with a specified type. This gives us the ability to provide the necessary context when appropriate.
Function composition is another use for reference types. By defining the operation on types, we can use the composition function itself on desired operands that are callable references.
The :: operator can be used for properties of first class. Property objects are of type KProperty<Int> and mutable properties are of type KMutableProperty<Int>. A property reference is a convenience over single generic  parameter function.

No comments:

Post a Comment