Pass-through varargs

My point there was merely it is not true that the compiler should always assume that when passing an array to format that it should be spread. Usually you probably want it to be spread, but it would be wrong to assume that is always the case.

And debugging where you are checking to see if 2 objects are the same object by looking at their toString representation which was the default toString is not hypothetical, I have done it. In that case it had nothing to do with arrays or format. I was just checking at different breakpoints and looking in the debugger variables window.

You can pass the array you just have to use the spread operator. Kotlin doesn’t discourage you, it just wants you to specify what you want to have happen. That is a good thing and I still have not heard any reasoning why you think Kotlin knows better and should take away that choice from you.

Inside the function it is treated exactly as an array. There is no distinction inside the function. Varargs is only about the caller of the function

Java doesn’t give you control. In some cases it assumes spread the array and in others it assumes it should pass the array as a single argument.

Consider a method foo that takes a single var arg parameter. In the case where we pass an array to it in java:

foo (anArray)

Java assumes that you want the array to be spread. If you didn’t want it to be spread Java doesn’t give you a way to specify that and your only solution is to make another array and put this array into it and pass that. Kotlin gives you the choice. By default it passes the array as a single value and if you want it to be spread you use the spread operator. If it passed the array as a spread how would you tell it to not spread it? Create an unspread operator? Force you to wrap it in another array like Java? I think Kotlin’s answer is best.

But take another case:

foo (anArray, aSingleValue)

In this case Java assumes the opposite and will pass the array as a single value and not spread the array. The only way to pass all the elements of the array and the additional value is to build a new array yourself and pass that. Kotlin gives you the choice to spread or not to spread.

To me your argument basically is that the default should have been to spread arrays passed to varargs and the * operator would be the unspread operator. It’s a little late to reverse direction and I think it is more logical that bar(array) always means passing the array as a single parameter whether the function parameter is varargs or not and spreading is the additional operator.

[quote=“saadfarooq, post:7, topic:2431”]
There might be valid reasons for spreading varargs, but this print out array hashcodes is not one of them.[/quote]

I assume you meant Not spreading here. Kotlin had to treat all varargs the same. It can’t do it one way for format and a different way for other varargs. That example was just to say that even for format you might want to pass an array without spreading it.

The bottom line is that when passing an array to a vararg sometimes you want to pass the array itself and sometimes you want to pass the elements of the array. Kotlin gives you a way to specify. The default is to treat the array the same way as if you were passing it to a non varargs function.

I see nothing that should be changed here.

1 Like