Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Demystifying Advanced Kotlin Concepts

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

Demystifying Advanced Kotlin Concepts

As we know Kotlin is one of the fastest growing language in the world. StackOverflow and Hackerrank developer surveys and Github insights bears testimony to the
prowess of this modern language. In Google I/O 2019, the company has gone for Kotlin-first in Android development.

This talk is designed for developers who have basic understanding of Kotlin.

Agenda of the talk:

- Generics in Kotlin.
- Extension and Infix Functions.
- Operator Overloading.
- Inline Function and Reified Type parameters.
- Sealed Class.

Avatar for Rishabh Maurya

Rishabh Maurya

June 06, 2019

Other Decks in Programming

Transcript

  1. • Kotlin is one of the most popular language today.

    • Developed by JetBrains in 2011, Ver. 1.0 released in 2016. • 100% interoperable with Java. • Google adopted Kotlin-First for Android in I/O 2019. • Last talk on “Kotlin: Introduction and Basics” by Arpit sir covered fundamentals and migration from Java to Kotlin; This talk is a continuation of it.
  2. • Generics in Kotlin • Extension and Infix Function •

    Operator overloading • Inline Functions and Reified Type Parameter • Sealed classes
  3. Generics Generics in Kotlin are a bit different from those

    in Java. It doesn’t have wildcards, it uses the concepts of declaration site variance and type projections instead. Variance is all about substituting type with subtypes or supertypes. There are two main type of variance:- • Covariance • Contravariance
  4. Covariance It means the type can be substituted by its

    subtypes but not by supertypes. In Java, it is implemented by <? extends T> . Its Kotlin equivalent is <out T>. e.g: Suppose Cow is subtype of Animal. Then List of animal can contain list of Animal as the immutable lists are covariant.
  5. Contravariance It means the type can be substituted by its

    supertypes but not by subtypes. In Java, it is implemented by <? super T> . Its Kotlin equivalent is <in T>. e.g: Suppose Cow is subtype of Animal. If we have a way to compare animals, then it should work on Cows too.
  6. Extension function To add additional functionality to pre-existing classes, extension

    functions are used. It does not modify class being extended but only makes new function callable with dot notation on the variable of that type. The type on which it is created is called receiver type and is available inside function scope as ‘this’.
  7. fun <T> List<T>.midElement(): T { if (isEmpty()) throw NoSuchElementException("List is

    empty.") return this[size / 2] } fun main(args: Array<String>) { var list = listOf<Int>(1, 2, 3, 4, 5) var mid = list.midElement() print(mid) } Sample Implementation
  8. Infix Function These functions can be called using the infix

    notation without parenthesis or dot notation. Requirements for Infix Function: • It must be a member or extension function with single parameter. • It should not accept variable number of arguments and no default value.
  9. Operator Overloading Kotlin allows us to provide an implementations for

    a predefined set of operators on our types. To implement an operator, we provide the member or extension function with a fixed name (which is unique for each operator), for the corresponding type. Function that overloads operator must be prefixed with operator modifier.
  10. Inline Function It is bit different from regular function. It

    will be substituted by its code during compilation, instead of doing the real call to the function. Reduces memory allocations and runtime overheads. Without inline modifier, a HOF having a function as a parameter will create an object that contains the function.
  11. Reified Type Parameter Declaring a type as reified allows us

    to use that type within the function. Function using it must be inline as the code needs to be replaced from which it is run in order to have access to the type. Just add reified modifier in front of type and you are good to go. e.g. <reified T>
  12. Sealed Class Sealed classes are used to represent restricted class

    hierarchies when a value can have one of the types from limited set. It is an extension of enums. The obvious place for using it is to make access to data that is only present in some circumstances a compile-time concern. The key benefits of using it comes into play when used with ‘when’ expression.