https://kotlinlang.org logo
Title
r

Ruckus

05/27/2021, 5:14 PM
Is there any reason to prefer
fun main(args: Array<String>)
over
fun main(vararg args: String)
?
r

randomcat

05/27/2021, 5:21 PM
The first is idiomatic
Also, how often are you planning to call
main
explicitly?
r

Ruckus

05/27/2021, 5:52 PM
What do you mean by "call
main
explicitly"? I pass parameters into many of my programs regularly when I run them.
r

randomcat

05/27/2021, 5:53 PM
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

Ruckus

05/27/2021, 5:55 PM
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

randomcat

05/27/2021, 5:55 PM
If you want to you can, but I'd recommend sticking with the idiomatic form
r

Ruckus

05/27/2021, 5:58 PM
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

randomcat

05/27/2021, 6:32 PM
np
s

Shawn

05/27/2021, 6:37 PM
@randomcat why is it idiomatic?
r

randomcat

05/27/2021, 6:39 PM
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

Shawn

05/27/2021, 6:50 PM
ah but that’s not Kotlin tho
r

randomcat

05/27/2021, 6:51 PM
So? Kotlin has its roots in Java
Idioms from there can sometimes carry over
s

Shawn

05/27/2021, 6:51 PM
For sure, I just wanna know what you think is more idiomatic about this one language feature instead of this other language feature
r

randomcat

05/27/2021, 6:52 PM
I'm not deciding what is idiomatic, I'm just saying what it the more common version
s

Shawn

05/27/2021, 6:53 PM
That seems different from saying “X is idiomatic [and therefore, Y is not]“, doesn’t it?
r

randomcat

05/27/2021, 6:53 PM
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

Shawn

05/27/2021, 6:54 PM
Definitely more common, agreed
r

Ruckus

05/27/2021, 6:54 PM
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

Alex Nordlund

05/28/2021, 9:36 AM
It also makes searching for main easier for people who are new to your project
r

Ruckus

05/28/2021, 2:37 PM
I hadn't thought of that. That's a good point.
s

Sourabh Rawat

06/01/2021, 3:42 PM
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!"