Skip to Content
Kotlin Cookbook
book

Kotlin Cookbook

by Ken Kousen
November 2019
Intermediate 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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Kotlin Programming Cookbook

Kotlin Programming Cookbook

Aanand Shekhar Roy, Rashi Karanpuria
Programming Kotlin

Programming Kotlin

Stephen Samuel, Stefan Bocutiu
Programming Kotlin

Programming Kotlin

Venkat Subramaniam

Publisher Resources

ISBN: 9781492046660Errata Page