https://kotlinlang.org logo
Title
m

marcinmoskala

09/28/2018, 10:17 AM
Why vararg Nothing is forbidden?
c

cketti

09/28/2018, 12:23 PM
If the goal is to force callers to use named parameters you should be able to accomplish that by creating your own class that you won't allow anyone to instantiate.
j

Jonathan

09/28/2018, 1:04 PM
Because it is not possible to get an instance of Nothing, using it in the argument of a function mean you don't want users to be able to call the function... Or (in this case) you you don't want users to pass any
ign
argument, so why declaring the argument in the first place?
m

marcinmoskala

09/28/2018, 1:23 PM
As @cketti guessed, to force users to use named parameters.
Using
Nothing
would be easier and more clear 😕
c

cketti

09/28/2018, 1:26 PM
the fact that many people didn't pick up what you're trying to accomplish probably means it's not very clear 🙂
you could name the type
ForceCallersToUseNamedParameters
. that'll make it somewhat clear
m

marcinmoskala

09/28/2018, 1:28 PM
😛
I am just experimenting with this idea. I took it from "Effective Python" where author suggested that pattern to force named parameters usage. Though in Python all you need it
*,
. Also I think that it is not in the philosophy of Kotlin. In Kotlin, function user should decide if he prefers more or less explicitly. Sometimes it would be redundant if user would extract variable with the same say anyway. Though good rationale is that this way function designer can always change parameters order. On the other hand for this we now have inline classes that in most cases should help us from passing value to a wrong parameter... It is not so simple and I am just experimenting now 😄
Formally in JVM
Nothing
parameter is translated to
Void
so it should work with
vararg
. No one really answered the question: why it is forbidden?
c

cketti

09/28/2018, 2:27 PM
it doesn't make much sense to allow creating a function that you can't call. you found an edge case where it could be possible to call the method. supporting that doesn't seem worthwhile from a language standpoint
👍 1
a

Allan Wang

09/28/2018, 2:35 PM
You could also just make an object (with a long name like
ForceNamedParameters
as mentioned above), and take that as input. It better describes the situation
object ForceNamedParameters

fun aaa(forceNamedParameters: ForceNamedParameters = ForceNamedParameters, a: Int, b: Int) {
...
}
j

Jonathan

09/28/2018, 2:44 PM
I'd declare a class with private constructor:
class ForceNamedParameters private constructor()
. But even with a properly named parameter, declaring an argument which is not an actual argument obfuscate the code IMO. I think it's up to the user of the function to decide weather it is worthwhile or not to use named parameters. I don't understand the needs to enforce it.
In most (all?) cases types of arguments should be enough for safe usage of the function. Simply avoid the primitive obsession smell
l

louiscad

09/28/2018, 6:30 PM
Actually, if
vararg something: Nothing
was allowed, you could call it because you could call it with zero parameters. It's just an
Array<Nothing>
under the hood, which can be empty.