January 2018
Intermediate to advanced
434 pages
14h 1m
English
What if we want the first argument to have a default value, like in this example:
fun main(args: Array<String>) { someMethod("3", "as","you","know","this","works")}fun someMethod(b: String = "x", vararg a: String) { println("b: " + b) for (a_ in a) { println(a_) }}
We want all arguments to be considered as part of vararg, but the compiler reads the first argument as b. In this case, naming the passed arguments can solve the problem:
fun main(args: Array<String>) { someMethod(a = *arrayOf("3", "as","you","know","this","works"))}fun someMethod(b: String = "x", vararg a: String) { println("b: " + b) for (a_ in a) { println(a_) }}
In the preceding code, the compiler understands that the value of b is not passed, and it takes ...
Read now
Unlock full access