liveBook · Manning

Chapter 3: Defining and calling functions

What you will learn in chapter 3 of Kotlin in Action, 2E

Introduces the chapter’s focus on making Kotlin functions easier to declare and call, and on adapting Java libraries to Kotlin style.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces the chapter’s focus on making Kotlin functions easier to declare and call, and on adapting Java libraries to Kotlin style.

3.1 Creating collections in Kotlin

This section introduces creating Kotlin collections and explains how they use Java collection classes while offering Kotlin operations on them.

Key terms: setOf, set, list, map, standard Java collection classes, collection interfaces, read only, last, sum, to

3.2 Making functions easier to call

The section motivates a customizable collection-formatting function and describes its initial implementation before later changes to make it easier to call.

Key terms: toString, joinToString, StringBuilder, separator, prefix, postfix, generic, collections

3.2.1 Named arguments

This section shows how named arguments make Kotlin function calls easier to read and can allow arguments to be reordered.

Key terms: named arguments, parameter names

3.2.2 Default parameter values

This section shows how default parameter values reduce the need for Kotlin overloads, how callers omit arguments, and how to support Java callers.

Key terms: default values, overloads, joinToString, regular call syntax, named arguments, trailing arguments, call site, @JvmOverloads, Java overloaded methods

3.2.3 Getting rid of static utility classes: Top-level functions and properties

This section shows how Kotlin replaces static utility classes with top-level functions and properties, and how those declarations appear to Java callers.

Key terms: top-level functions, package, static methods, Kt suffix, @file:JvmName, top-level properties, static field, accessor methods, const

3.3 Adding methods to other people’s classes: Extension functions and properties

Introduces extension functions as a way to call functions on existing classes with member-like syntax without changing those classes.

Key terms: extension function, member, final, receiver type, receiver object, this, receiver, private, protected, encapsulation

3.3.1 Imports and extension functions

This section explains how extension functions are imported and how import aliases resolve naming conflicts between them.

Key terms: extension function, import, * imports, as keyword, short name, fully qualified name

3.3.2 Calling extension functions from Java

Explains how Kotlin extension functions appear as static methods to Java callers and how to invoke them.

Key terms: extension function, receiver object, static method, static import

3.3.3 Utility functions as extensions

This section turns joinToString into a collection extension with default arguments and explains two consequences of extensions’ static nature.

Key terms: joinToString, extension, collection, default values, extension functions, static method calls, receiver type, collections of strings, overridden

3.3.4 No overriding for extension functions

This section contrasts overriding of member functions with static resolution of extension functions and explains why a member function takes precedence over an extension with the same signature.

Key terms: extension function, member function, declared static type, run-time type, receiver, same signature, shadowed

3.3.5 Extension properties

This section shows how extension properties provide property-style access to existing classes without adding stored state, including mutable properties and Java access.

Key terms: extension property, receiver type, custom accessors, backing field, var, StringBuilder, getter, setter, getLastChar, setLastChar

3.4 Working with collections: varargs, infix calls, and library support

This introduction previews collection-related standard-library functions and language features covered in the following subsections.

3.4.1 Extending the Java collections API

This subsection explains how Kotlin adds collection operations to Java collection classes through standard-library extension functions and how to discover those operations.

Key terms: extension functions, last, sum, Kotlin standard library, code completion, standard library reference, regular methods

3.4.2 Varargs: Functions that accept an arbitrary number of arguments

This section explains how Kotlin functions accept a variable number of arguments and how to pass array elements as those arguments.

Key terms: varargs, vararg modifier, spread operator, *

3.4.3 Working with pairs: Infix calls and destructuring declarations

This section explains how infix calls create pairs for maps and how destructuring declarations unpack pairs and other values.

Key terms: infix call, infix modifier, to, extension function, to function, Pair, mapOf, key, value, destructuring declaration

3.5 Working with strings and regular expressions

The section explains that Kotlin and Java use the same strings, while Kotlin offers a clearer API for working with them.

Key terms: Kotlin strings, Java strings, Kotlin standard library methods, extension functions, standard Java strings

3.5.1 Splitting strings

Explains why Java’s string splitting can be surprising and how Kotlin distinguishes regular-expression splitting from splitting on literal delimiters.

Key terms: split, regular expression, Regex, Pattern, split extension function, plain-text strings, character arguments, delimiters

3.5.2 Regular expressions and triple-quoted strings

Shows two ways to parse a file path into a directory, filename, and extension, including how triple-quoted strings and match results work in the regular-expression approach.

Key terms: substring before, substring after, delimiter, String extensions, triple-quoted string, regular expression, groups, literal dot, match, destructured property

3.5.3 Multiline triple-quoted strings

Explains how multiline triple-quoted strings preserve text, how to format and interpolate them, and why they are useful for expected output in tests.

Key terms: triple-quoted strings, multiline string, escape sequences, trimIndent, common minimal indent, string templates, embedded expression, escaped Unicode symbol, expected output, multiline strings

3.6 Making your code tidy: Local functions and extensions

This section shows how local functions and extension functions can remove duplicated validation code while keeping its purpose and scope clear.

Key terms: local function, don’t repeat yourself (DRY), validation, local functions, enclosing function, parameters, variables, extension function, User.validateBeforeSave, private data

3.7 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 2: Kotlin basics Chapter 4: Classes, objects, and interfaces →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.