Is there any reason to prefer `fun main(args: Arra...
# getting-started
r
Is there any reason to prefer
fun main(args: Array<String>)
over
fun main(vararg args: String)
?
r
The first is idiomatic
Also, how often are you planning to call
main
explicitly?
r
What do you mean by "call
main
explicitly"? I pass parameters into many of my programs regularly when I run them.
r
I mean how often are you calling
main
directly from Kotlin code
That's the only time when allowing or disallowing varargs would affect it
r
Ah, I see what you're saying. Indeed, I basically never call
main
directly, but for some strange reason I find the
vararg
variant far more aesthetically pleasing when reading the source.
r
If you want to you can, but I'd recommend sticking with the idiomatic form
r
Fair enough. In general I tend to prefer idiomatic constructs, so that's compelling reason to stick with `Array`s. But the
vararg
just looks so good for some reason... 🙂
Anyway, thanks for the (admittedly rather pointless) discussion.
r
np
s
@randomcat why is it idiomatic?
r
The idiomatic way to write it in Java is
public static void main(String[] args)
.
fun main(args: Array<String>)
is the direct translation of that into Kotlin. It's also how the docs define
main
https://play.kotlinlang.org/byExample/01_introduction/01_Hello%20world
s
ah but that’s not Kotlin tho
r
So? Kotlin has its roots in Java
Idioms from there can sometimes carry over
s
For sure, I just wanna know what you think is more idiomatic about this one language feature instead of this other language feature
r
I'm not deciding what is idiomatic, I'm just saying what it the more common version
s
That seems different from saying “X is idiomatic [and therefore, Y is not]“, doesn’t it?
r
I mean I was taking "idiomatic" to mean commonly used and accepted
I think the first form is by far more common than the varargs form
s
Definitely more common, agreed
r
Also, the fact that
maina
in IntelliJ generates the
Array
version, it's probably safe to assume it's considered the more idiomatic of the two.
a
It also makes searching for main easier for people who are new to your project
r
I hadn't thought of that. That's a good point.
s
I would say
Array
is much easier to understand that vararg. "Hey it's an array of arguments" Vs "Hey it's vararg? Hmm whats the type of it? Is it list? Array? Never mind, at least it says several args, so an Iterable, aha!"