Kotlin in Action : Chapter 3, Defining and Calling Functions

Caren Chang
3 min readApr 30, 2017

--

Yet another week of reading of Kotlin in Action! Chapter 3 goes into depth about functions in Kotlin.

(Check out summaries of other chapters all listed here: https://medium.com/@calren24/reading-through-kotlin-in-action-428111b051ce)

Kotlin uses standard Java collection classes

// kotlin code, example of creating collections
val set = hashSetOf(1, 2, 3)
val list = arrayListOf(1, 2, 3)
// we can see here that Kotlin is just using the standard Java
// classes we are already familiar with
>>> println(set.javaClass)
class java.util.HashSet
>>> println(list.javaClass)
class java.util.ArrayList
  • this makes it easier to interact with existing Java code since you don’t need to convert collections with interacting between Java and Kotlin

Named arguments

  • One of the big code smells you often find in Java is functions that take in too many parameters. Eventually, when you call said function, you end up with something like:
someFunction(collections, true, false, true, 3);
  • the above example makes it hard to read the code and know what’s going on. Builders are recommended in these situations
  • In Kotlin, the you can call functions with named arguments like such:
someFunction(collections, isColors = true, isPrimaryColors = false, numberOfSubColors = 3)
  • Being able to use named arguments is made even better since it allows you to specify arguments in arbitrary order and also leave out arguments. this reduces the number overloading methods/constructors
fun <T> joinToString(
collection: Collection<T>,
separator: String = ", ",
prefix: String = "",
postfix: String = ""
): String
// without creating extra overloading methods, we can do something
// like such and have the undefined args go to the default value:
joinToString(listOfStrings, prefix = "pre");

Kotlin allows functions to be placed at the top level of a source file outside of a class

  • this reduces the need to create all the “Util” classes for static helper methods that don’t really belong anywhere

Kotlin has something called extension functions that allows you to define extra functions on top of existing ones

  • the above statements is probably confusing, but the following example makes it a bit clear
// since Kotlin projects are built on top of Java libraries, we can 
// import the commonly used libraries
package strings
// we can further build on top of the strings library as such
fun String.lastChar(): Char = this.get(this.length - 1)
// what the line above is actually doing is creating a static method // that accepts the object as its first arg// and then call the function we just created
>>> println("caren".lastChar())
n

Kotlin has some extra useful features when working with collections such as vararg, infix, destructing declarations

  • vararg : declares function for taking arbitrary number of args
  • infix : provide a clean syntax for calling operator-like methods with a single argument.
// this function declaration is prefixed with the infix keyword
infix fun Any.to(other: Any) = Pair(this, other)
// thanks to the infix function from above, we can now do the
// following

val numbersMap = mapOf(1 to "one", 2 to "two", 53 to "three")
// this is what the same line of code would look like without the
// infix function

val numbersMap = mapOf(1.to("one"), 2.to("two"), 3.to("three));
// although this is a trivial example, more complicated situations // would better prove the power of infix functions
  • destructing declarations: unpacks single composite value into multiple variables
infix fun Any.to(other: Any) = Pair(this, other)// Kotlin allows you to initialize 2 variables of a Pair object
val (number, name) = 1 to "one"
  • Kotlin has a lot of extra methods when working with strings (subStringBeforeLast(), subStringAfterLast(), split() takes in multiple string args, and many more)

Chapter 1: What and Why
Chapter 2: Kotlin Basics

--

--