/ Kotlin

Kotlin Basics: Named Parameters and Default Arguments

Named parameters and default parameter values

Similar to their equivalents in Python and Scala, functions in Kotlin can have named parameters and default parameter values. This makes it easy to provide an arbitrary order of params, by specifiyng them by name. It also allows for skipping certain params altogether, when we do not need to specify them. When declaring a function, this leads to less overhead with overloads. Check out the function joinToString(...) provided to all collections:

 * Creates a string from all the elements separated using [separator] and using the given [prefix] and [postfix] if supplied.
 * If the collection could be huge, you can specify a non-negative value of [limit], in which case only the first [limit]
 * elements will be appended, followed by the [truncated] string (which defaults to "...").
public fun <T> Iterable<T>.joinToString(
		separator: String = ", ",
		prefix: String = "",
		postfix: String = "",
		limit: Int = -1,
		truncated: String = "...",
		transform: ((T) -> String)? = null): String {
    return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString()

when you only want to add values to one or two parameters, you can do like so:

collection.joinToString(prefix = "{", postfix = "}")

Default Arguments
Named Parameters