avatarGabriel Shanahan

Summary

The provided web content is an excerpt from a "Kotlin Primer" article series, focusing on variable number of arguments in Kotlin functions using the vararg keyword and the spread operator *.

Abstract

The article introduces the concept of variable numbers of arguments in Kotlin, a feature that allows functions to accept an indefinite number of parameters. It explains how to define such functions using the vararg keyword and how the vararg parameter is treated as an Array<out T>. The article also covers the use of the spread operator * to pass arrays as multiple arguments to vararg functions, a feature distinct from Java's handling of variable arguments. Interactive Kotlin playground examples are embedded to demonstrate the concepts discussed. The article is part of the "Kotlin Primer" series aimed at facilitating Kotlin adoption in Java-centric organizations and is supported by Etnetera a.s., with gratitude expressed for their support. Readers are encouraged to read the introduction and refer to the table of contents for the full series.

Opinions

  • The author expresses a preference for Kotlin's approach to variable numbers of arguments over Java's, highlighting the convenience of the spread operator.
  • The article is positioned as an opinionated guide, suggesting that the content reflects the author's perspective on best practices for Kotlin adoption.
  • The author emphasizes the importance of understanding variance with out and in types, which is crucial for working with vararg parameters in a type-safe manner.
  • The use of interactive Kotlin playgrounds indicates the author's belief in the value of hands-on learning and immediate practical application of concepts.
  • The acknowledgment of Etnetera a.s. suggests a collaborative approach to learning and adopting Kotlin within an organizational context.

Variable Number of Arguments

Introduction to vararg and the spread operator

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #FYI

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

The vararg keyword

Like Java, Kotlin permits functions with variable number of arguments. Such parameters are declared as vararg, i.e. fun myFun(vararg xs: Int).

For example:

The type of the vararg parameter is Array<out T>. We haven't talked about out and in, which have to do with variance. Basically, out means that x can also contain subtypes of T. In other words, everything behaves just like a function with a fixed number of arguments would — for example, you can pass an Int to the function fun myFun(x: Number).

The spread operator

Imagine you have a sum function defined:

It could naturally happen that you have a list of numbers that you want to pass to this function:

Unlike Java, Kotlin has the spread operator, denoted *. This allows you to "splice" an array (not a list!) into a vararg parameter:

Both of these are mentioned in the docs.

Go back to Tail Recursive Functions, jump to the Table of Contents, or continue to Classes — What We Know From Java.

Join me in Etnetera
Kotlin
Java
Programming
Vararg
Learning
Recommended from ReadMedium