Home » JVM Languages » Kotlin » Kotlin infix functions

About Michael Scharhag

Michael Scharhag
Michael Scharhag is a Java Developer, Blogger and technology enthusiast. Particularly interested in Java related technologies including Java EE, Spring, Groovy and Grails.

Kotlin infix functions

What are infix functions?

If you are using Kotlin chances are high you already used infix functions. Maybe without knowing it.

When calling infix functions it is possible to omit the dot and the parentheses. So instead of

1
car.move(forward)

we can write:

1
car move forward

Here are a few examples of commonly used infix functions in Kotlin. The standard call notation (including dot and parentheses) for these functions is shown as comment.

01
02
03
04
05
06
07
08
09
10
11
12
val map = mapOf(
    "foo" to 42     // "foo".to(42)
)
 
for (i in 0 until 5) {  // 0.until(5)
    ...
}
 
val regex = Regex("\\w")
if ("foo" matches regex) {  // "foo".matches(regex)
 
}

Writing your own infix function

Of course we can write our own infix functions.

Infix functions must satisfy the following requirements:

  • They must be member functions or extension functions
  • They must have a single parameter
  • The parameter must not accept variable number of arguments and must have no default value

To create an infix function we have to add the infix keyword to the function signature.

For example:

01
02
03
04
05
06
07
08
09
10
enum class TurnDirection { left, right }
 
class Car {
    infix fun turn(turnDirection: TurnDirection) { ... } // our infix function
}
 
fun main() {
    val car = Car()
    car turn left // infix function call, static import of "left" required   
}

If we return an object that contains an infix function itself, we can chain infix function calls.

For example:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
class TurnDirectionBuilder {
    infix fun then(turnDirection: TurnDirection) { ... }
}
 
class Car {
    infix fun turn(turnDirection: TurnDirection): TurnDirectionBuilder {
        return TurnDirectionBuilder()
    }
}
 
fun main() {
    val car = Car()
    car turn left then right // same as: car.turn(left).then(right)
}

A common example of this in Kotlin is the IntProgression.step() function that might be used in for loops.

For example:

1
for (i in 0 until 10 step 2) { .. }  // same as: 0.until(10).step(2) 

Published on Java Code Geeks with permission by Michael Scharhag, partner at our JCG program. See the original article here: Kotlin infix functions

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments