Skip to Main Content
Kotlin Cookbook
book

Kotlin Cookbook

by Ken Kousen
November 2019
Intermediate to advanced content levelIntermediate to advanced
254 pages
4h 55m
English
O'Reilly Media, Inc.
Content preview from Kotlin Cookbook

Chapter 11. Miscellaneous

This chapter consists of recipes that don’t fit any of the other headings. Here you’ll find how to make the when function exhaustive, how to measure the elapsed time of a function, and how to use the TODO function from the standard library, among many others.

11.1 Working with the Kotlin Version

Problem

You want to find out programmatically which version of Kotlin you are currently using.

Solution

Use the CURRENT property in the companion object of the KotlinVersion class.

Discussion

Since version 1.1, the kotlin package includes a class called KotlinVersion that wraps the major, minor, and patch values for the version number. Its toString method returns the combination as major.minor.patch given an instance of this class. The current instance of the class is contained in a public field called CURRENT in the companion object.

It’s therefore trivially easy to return the current Kotlin version. Just access the field KotlinVersion.CURRENT, as in Example 11-1.

The result is the major/minor/patch version of the Kotlin compiler, such as 1.3.41. All three parts of this quantity are integers between 0 and MAX_COMPONENT_VALUE, which has the value 255.

Note

The CURRENT property is annotated as a public @JvmField in the source code, so it is available from Java as well.

The KotlinVersion class implements the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Functional Kotlin

Functional Kotlin

Mario Arias, Rivu Chakraborty
Kotlin Programming Cookbook

Kotlin Programming Cookbook

Aanand Shekhar Roy, Rashi Karanpuria
Functional Programming in Kotlin

Functional Programming in Kotlin

Runar Bjarnason, Paul Chiusano, Marco Vermeulen
Learning Concurrency in Kotlin

Learning Concurrency in Kotlin

Miguel Angel Castiblanco Torres

Publisher Resources

ISBN: 9781492046660Errata PageSupplemental Content