avatarGabriel Shanahan
# Summary

The provided web content is an article discussing the use of infix functions in Kotlin, a feature that allows for the creation of functions that can be called with an infix notation, enhancing code readability and conciseness.

# Abstract

The article offers a concise demonstration of infix functions in Kotlin, emphasizing their role in enabling developers to write more expressive and readable code. It explains that infix functions are methods with a single parameter that can be invoked without the dot or parentheses, placing the function name between the operands. The article highlights the `to` function as a prime example, which creates a `Pair<A, B>` from two operands. Infix functions are defined as members of a class or as extension functions and require exactly two parameters, with the class acting as the left operand and the method parameter as the right. The article is part of the "Kotlin Primer," intended to facilitate Kotlin adoption in Java-centric organizations and was originally crafted as a learning resource for Etnetera a.s. Readers are encouraged to read the introduction and refer to the table of contents for further articles in the series.

# Opinions

- The author considers infix functions as syntactic sugar, implying that while they enhance the language's expressiveness, they are not a fundamental feature but rather a convenient one.
- The article suggests that the Kotlin language is designed with a focus on developer experience, by providing features like infix functions that make the code more intuitive and closer to natural language.
- The author expresses gratitude to Etnetera a.s. for supporting the creation of the "Kotlin Primer," indicating a collaborative relationship and a shared interest in promoting Kotlin within the Java community.

Infix Functions

A quick demonstration of infix functions

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

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.

Kotlin allows you to define infix functions, giving you the ability to call functions in an infix manner (i.e. the function name goes between the operands). A great example is the to function, which we mentioned briefly in the article on data classes. It creates a Pair<A, B> from its left and right operands. At it's core, it's nothing but syntactic sugar allowing you to omit the . and () for these functions, e.g. myObj.operation(arg) becomes myObj operation arg.

Every infix function needs exactly 2 parameters — the one on the left and the one on the right. For this reason, infix functions are always defined as methods on some class (or extension functions, but we’ll talk about those later). The class represents the argument on the left. For a method to be declared infix, it must have exactly one parameter, which represents the argument on the right.

Go back to Enums, jump to the Table of Contents, or continue to Operators.

Join me in Etnetera
Kotlin
Java
Programming
Infix
Infix Function
Recommended from ReadMedium