Five things you might not know in Kotlin

Piotr Mądry
ProAndroidDev

--

  1. Pair & Triple.
  • Infix function as the speed boost for creating Pairs.

Functions marked with the infix keyword can also be called using the infix notation (omitting the dot and the parentheses for the call).

  • The biggest disadvantage of using those two was not always clear what .first, .second, .third mean. Thankfully to the destructuring declaration, it is not a problem anymore.

2. Spread operator as nice to know Kotlin feature!

You can simply pass an array to your vararg with Spread operator.

Merging two collections with Spread operator is simple as it.

3. Sealed classes are Enum classes on steroids!

Sealed classes are used for representing restricted class hierarchies, when a value can have one of the types from a limited set, but cannot have any other type. They are, in a sense, an extension of enum classes: the set of values for an enum type is also restricted, but each enum constant exists only as a single instance, whereas a subclass of a sealed class can have multiple instances which can contain state.

They are so good! I use them almost every day. This is the foundation under my daily coding challenges so often because…

  • Helps me to distinguish errors which I’m expecting.
  • Introduces types which improve code readability.
  • Describes the state of object/feature/animation.

And they are pretty easy to work with!

4. Delegates for widgets

Really helpful if we want to reflect the variable manipulations on a view

Now, every change on pages in presenter will update the text in book_fragment_pages TextView.

5. Reified types to avoid boilerplate!

If we want to access a type without passing class as a parameter in a function.

If you like my work hit 👏 button and let me know what you think in comments!
Stay in touch on twitter!

--

--