The Programmers Guide To Kotlin: Advanced Functions
The Programmers Guide To Kotlin: Advanced Functions
Written by Mike James   
Monday, 11 September 2017
Article Index
The Programmers Guide To Kotlin: Advanced Functions
this & Extension Functions
Infix Functions


Infix Functions

A very nice and very simple syntactic transformation is to convert:

object1 function object2



This allows you to write functions as infix operators.

This only works for methods, of course, and extension functions because it has to be possible to call the function with a sensible this value. It also only works with functions with a single parameter.

For example, we can define an infix extension function’s sum:

infix fun Int.sum(a:Int):Int {
 return this+a

Now we can write:

1 sum 2

and get the result 3.

It is obvious, from the fact that you can use a function call in an expression, that infix functions can be used in expressions in a natural manner.

For example:

1 sum 2 * 3

is seven as multiplication has a higher priority than a function call.

Function Types

In most cases all you really need to know is the signature of a function, i.e. what the types are of its parameters and perhaps its return type. The signature of a function is used to determine which function is called if there are a number of overloads.

Sometimes, however, we need to define a type that corresponds to a particular signature and return type. For this we need to create a specific function type. 

For example:


defines a function type that takes a String and returns an Int.



defines a function type that takes two Ints and returns an Int. As already introduced in Chapter 7 we can define a type alias to avoid having to type out a function type definition each time it is needed.

For example:

typealias arith =(Int,Int)->Int

Defines the arith type to be a function that takes two Ints and returns an Int.

The only not quite obvious function type is:


which is the type of a function taking no parameters and returning no result.


  • Kotlin gives the impression that you can define functions outside of classes i.e. functions that are not methods, but in fact it converts these into methods of a default class.

  • You can set the name of the default class using:

    @file:JvmName("class name")

  • Local functions can access the containing functions variables but not vice versa.

  • All parameters are passed by value and are read-only val types.

  • As object references are passed as read-only values, referencing the this object means you can change properties on an object from within a function.

  • You can also use default, named and vararg parameters.

  • The receiver or call context is represented by this in methods.

  • For nested classes this can be qualified by the name for the class for which the call context is required.

  • Extension functions can make use of this to make them look as if they are methods of an existing class, but they cannot access the internal workings of the class.

  • Methods and extension functions with a single parameter can be used as infix operators with the object to the left as the call context, and the object on the right as the single parameter.

  • You can define a function type as the signature and return type of the function.



Programmer's Guide To Kotlin

Now Available as a Print Book


You can buy it from:

USA and World




Some Chapters Already Available On The Web

  1. What makes Kotlin Special (Book Only)
  2. The Basics: Variables, Primitive Types and Functions
  3. Control
  4. Strings and Arrays
  5. The Class & The Object
  6. Inheritance
  7. The Type Hierarchy 
             Extract  Type and its problems
  8. Generics
             Extract  Basic Generics **NEW!**
  9. Collections, Iterators, Sequences & Ranges
             (Coming soon!)
  10. Advanced functions 
  11. Anonymous, Lamdas & Inline Functions
  12. Data classes, enums and destructuring
             Extract  Data Classes 
  13. Exceptions, Annotations & Reflection
             Extract Annotation & Reflection 
  14. Working with Java


To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on, Twitter, FacebookGoogle+ or Linkedin.



GitHub Education A Free Program

GitHub has a new program that aims to get GitHub adopted by educational and training institutions. The GitHub Education bundle includes free access to GitHub Enterprise and the support offered by [ ... ]

Why Article 13 Must Be Stopped

With only days to go before a crucial vote on EU copyright legislation could have a drastic impact on the internet and, as many others have pointed out, spell the end for memes, Internet luminaries ha [ ... ]

More News





or email your comment to:




Last Updated ( Monday, 11 September 2017 )

RSS feed of all content
I Programmer - full contents
Copyright © 2018 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.