Kotlin allows you to provide function parameters with a default value, which is used if the corresponding argument is omitted on the call.
fun String.split(separator: Char = ',')
If you want to split a string with the above (extension) function, you can determine the separator yourself, but thanks to the default value
, also omit it, if the
, is actually what you need:
str.split('|') // splits str on | str.split() // splits str on ,
Default parameters are particularly interesting in terms of class members, since with their help you can (often) do without the otherwise commonly used overloading of methods in Java. This leads to more compact, more readable code and less boilerplate.
Especially in combination with named arguments, default arguments are a powerful tool for more concise code.