avatarGabriel Shanahan

Summary

The web content discusses how to define operators as extension functions in Kotlin, which can eliminate the need for creating new types when implementing specific behaviors like list destructuring.

Abstract

The article delves into the advanced Kotlin feature of defining operators as extension functions, illustrating how this approach can simplify code by allowing developers to add new behaviors to existing types. It provides practical examples using Kotlin Playground to demonstrate how to implement destructuring of a list into head and tail elements without the need to define a new list type, such as FunctionalList<T>. This method leverages the component1 and component2 operators and is part of a larger series called the Kotlin Primer, aimed at facilitating Kotlin adoption in Java-centric organizations. The article emphasizes the elegance and efficiency of using extension functions for operator overloading, avoiding the complexity of type conversion.

Opinions

  • The author advocates for the use of extension functions to define operators, suggesting it as a best practice in Kotlin development.
  • There is an appreciation expressed for the Kotlin language's flexibility, which allows for such extensions, enhancing the language's expressiveness.
  • The article implies that the ability to define operators as extensions is a significant feature that can lead to cleaner and more maintainable code.
  • The author seems to value the destructuring pattern for lists and views the ability to apply it directly to List<T> as a major improvement.
  • There is a sense of gratitude towards Etnetera a.s. for supporting the creation of the Kotlin Primer series, indicating a collaborative environment within the Kotlin community.

Operators as Extensions

How to define operators as extensions, and how it can save you from defining new types.

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #KOTLIN FEATURE

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

Continuing on our tour of the interactions between extension functions and other Kotlin features — you can define operators as extension functions:

However, (and this is true regardless of extensions) when the types are different, you need to be careful about the order of the operands:

In the previous article, we created a new implementation of List<T>, which we called FunctionalList<T>, which allowed itself to be destructured into a head and a tail by defining the component1 and component2 operators.

Here it is for reference:

We applied some kotlin-fu which allowed us to easily use our implementation wherever a regular list was expected, but even though we managed to do a pretty good job, we were still forced to create an entirely new type for this task. As a consequence, we also had to convert a regular List<T> into our FunctionalList<T> before we could use our destructuring approach — that’s what the asFunList extension function is for.

But if you think about it, all we really wanted to do was define the component1/2 operators on List<T> itself. Armed with the knowledge that operators can be defined as extension functions, we can completely bypass the need for the separate type:

Much cleaner, isn’t it?

Go back to Extension Function Applications: Generic Receivers — continued, jump to the Table of Contents, or continue to Scope Functions: Introduction.

Join me in Etnetera
Kotlin
Programming
Java
Object Oriented
Functional Programming
Recommended from ReadMedium