, vararg ( ), :
fun sum(vararg data:Int)
{
}
, , :
sum(1,2,3,4,5)
, , :
val array= intArrayOf(1,2,3,4,5)
then, to call this method, we must use the distribution operator, for example:
sum(*array)
Here * (distribution operator) will skip the entire contents of this array.
* array is equivalent to 1,2,3,4,5
But wait a minute, if we call it this: sum(array)
it will give us a type mismatch error:
Type mismatch.
Required:Int
Found:IntArray
The problem is that the function sumtakes a parameter vararg Int(which takes a value, for example: 1,2,3,4,5), and if we pass an array, it will be passed as IntArray.
source
share