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
.