Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin asterisk operator before variable name or Spread Operator in Kotlin

Tags:

syntax

kotlin

People also ask

What is Asterisk in Kotlin?

The * operator is known as the Spread Operator in Kotlin. From the Kotlin Reference...

Does Kotlin have a spread operator?

It's Kotlin's spread operator — the operator that unpacks an array into the list of values from the array. It is needed when you want to pass an array as the vararg parameter. it will fail with Type mismatch: inferred type is Array<String> but String was expected compilation error.

What is Vararg in Kotlin?

Variable Number of Arguments (Varargs) In Kotlin, a vararg parameter of type T is internally represented as an array of type T ( Array<T> ) inside the function body. A function may have only one vararg parameter.

What is the meaning of -> in Kotlin?

The -> is a separator. It is special symbol used to separate code with different purposes. It can be used to: Separate the parameters and body of a lambda expression val sum = { x: Int, y: Int -> x + y }


The * operator is known as the Spread Operator in Kotlin.

From the Kotlin Reference...

When we call a vararg-function, we can pass arguments one-by-one, e.g. asList(1, 2, 3), or, if we already have an array and want to pass its contents to the function, we use the spread operator (prefix the array with *):

It can be applied to an Array before passing it into a function that accepts varargs.

For Example...

If you have a function that accepts a varied number of arguments...

fun sumOfNumbers(vararg numbers: Int): Int {
    return numbers.sum()
}

Use the spread operator to pass an array's elements as the arguments:

val numbers = intArrayOf(2, 3, 4)
val sum = sumOfNumbers(*numbers)
println(sum) // Prints '9'

Notes:

  • The * operator is also the multiplication operator (of course).
  • The operator can only be used when passing arguments to a function. The result of the operation cannot be stored since it yields no value (it is purely syntactic sugar).
  • The operator may confuse some C/C++ programmers at first because it looks like a pointer is being de-referenced. It isn't; Kotlin has no notion of pointers.
  • The operator can be used in-between other arguments when calling a vararg function. This is demonstrated in the example here.
  • The operator is similar to the apply function in various functional programming languages.

In addition to the answers that were directly towards "what is this thing!?!", you often have the case where you have a List and want to pass it to a function that is expecting a vararg. For this, the conversion is:

someFunc(x, y, *myList.toTypedArray())

Assuming that last parameter of someFunc is vararg of the same type as the elements in the list.


As described in the documentation this is a spread operator:

When we call a vararg-function, we can pass arguments one-by-one, e.g. asList(1, 2, 3), or, if we already have an array and want to pass its contents to the function, we use the spread operator (prefix the array with *):

val a = arrayOf(1, 2, 3) 
val list = asList(-1, 0, *a, 4)

In Java you can pass an array as is but an advantage of unpacking an array with spread operator * is that spread operator lets you combine the values from an array and some fixed values in a single call. Java doesn't support this.


If a function which accept a vararg(Variable number of arguments) parameter like:

fun sum(vararg data:Int)
{
   // function body here         
}

Now to call this method, we can do:

sum(1,2,3,4,5)

But what if we have these value in an array, like:

val array= intArrayOf(1,2,3,4,5)

then, to call this method we have to use spread operator, like:

 sum(*array)

Here, *(spread operator) will pass all content of that array.

*array is equivalent to 1,2,3,4,5

But wait a minute, what if we call it like this: sum(array) it will give us Type Mismatch compile time error:

Type mismatch.
Required:Int
Found:IntArray

The problem is sum function accept a vararg Int parameter(which accept value like: 1,2,3,4,5) and if we pass array, it will be passed as IntArray.