We continue with the discussion on Kotlin versus Java:
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.
Kotlin allows operator overloading just like other languages with a binary or unary associativity, precedence and fixed symbolic representation or a name. The operator implementation is usually a member function or an extension function.
Certain types of operators can be overloaded while others cannot. All arithmetic operators can be overloaded. Equality and comparision operators can also be overloaded but not identity operators. Custom inflix operators can be simulated using function calls. Inflix is a keyword and is used with functions that take a single parameter without a default value. Influx functions require both the receiver and function parameter to be specified.
No comments:
Post a Comment