https://kotlinlang.org logo
#announcements
Title
# announcements
h

horse_badorties

08/18/2017, 8:32 AM
no, it doesn't. Nothing to do with vararg, it seems.
Copy code
fun foo(bool: Boolean = true, string1: String) {}
fun main(args: Array<String>) {
   foo("b") // does not compile
}
k

karelpeeters

08/18/2017, 8:34 AM
Then that's probably how the rules of default parameters work. Doesn't look like there's an explanation on the site.
h

hho

08/18/2017, 8:36 AM
yeah, it seems default values have to be "to the right" of given parameters (which makes sense, if you think that the types could be the same)
h

horse_badorties

08/18/2017, 8:41 AM
but this doesn't compile as well:
Copy code
fun foo(vararg strings: String, bool: Boolean = true) {}

fun main(args: Array<String>) {
    foo("a", "b", true) // does not compile
}
k

karelpeeters

08/18/2017, 8:41 AM
Yea that never works, you can't have anything after varargs.
h

horse_badorties

08/18/2017, 8:42 AM
so you can't mix vararg and default?
k

karelpeeters

08/18/2017, 8:42 AM
Well you can if you put the defaults before the vararg.
h

horse_badorties

08/18/2017, 8:42 AM
my bad, sorry
foo("a", "b", bool = true)
works, like documented
k

karelpeeters

08/18/2017, 8:43 AM
I think they disallow normal parameters after vararg because typing wouldn't work that well.
h

horse_badorties

08/18/2017, 8:44 AM
putting the default before the vararg was my initial example, @karelpeeters
k

karelpeeters

08/18/2017, 8:44 AM
Yea I meant in the function definition, then you can use a default parameter without naming it at the callsite.
h

horse_badorties

08/18/2017, 8:45 AM
like how?
k

karelpeeters

08/18/2017, 8:46 AM
Ah my bad, that doesn't work either.
3 Views