Learning Kotlin: Named Arguments

Submitted by Robert MacLean on Thu, 06/14/2018 - 09:00

More Information

On to our third exercise and definitely, the difficulty curve has lowered again (or should it be steep) as we have a simple lesson - how to use named arguments.

Not only is an example for it provided for how named arguments work:

  1. fun usage() {
  2.     // named arguments
  3.     bar(1, b = false)
  4. }

We also get an example of default values for arguments

  1. fun bar(i: Int, s: String = "", b: Boolean = true) {}

The problem we need to solve itself is simple too,

Print out the collection contents surrounded by curly braces using the library function 'joinToString'. Specify only 'prefix' and 'postfix' arguments.

Which has this answer:

  1. fun task2(collection: Collection<Int>): String {
  2.     return collection.joinToString(prefix = "{", postfix = "}")
  3. }

Not much to add to this lesson unfortunately.