Named arguments

Kotlin supports naming arguments when passing them to functions. This can make your code more verbose but can improve its readability.

Java doesn't have named function arguments, and the common approach in Java is to use the builder pattern when your class or function accepts multiple arguments. This makes the code even more verbose. Consider this regionMatches function call on the String type; it accepts five arguments and three of them are of the Int type:

String str = "foo";
boolean match = str.regionMatches(true, 0, "foo", 0, 3);

If you were reading this code for the first time, you'd probably have to look up this function documentation to see what all the arguments represent.

In Kotlin, thanks to named arguments, we can call the same function like this:

val str = "foo"
val match = str.regionMatches(thisOffset = 0, other = "foo", otherOffset = 0, length = 3, ignoreCase = true)

Now, we know what each argument represents without looking into the documentation of the Java standard library.

Another benefit of named arguments is that it makes your code less error-prone. Multiple arguments of the same type could easily be mistaken. With named arguments, this cannot happen.

If you use named arguments when calling a function, then you can place the arguments in any order you wish.

Also, when calling a function and using named arguments, you have to name all the arguments after the first named argument. This example names only one argument, otherOffset, without naming arguments that follow, and the compiler will not allow this:

val str = "foo"
//compiler error
val match = str.regionMatches(3, "foo", otherOffset = 0, 3, true)

But, this example compiles because all arguments after the first named one are also named:

val str = "foo"
val match = str.regionMatches(3, "foo", otherOffset = 0, length = 3, ignoreCase = true)

Named arguments can also be used on class constructors. If we have a class declaration like this:

class User(private val firstName: String,
private val lastName: String,
private val birthYear: Int)

We can then reverse the order of arguments when calling the constructor if we wish, as you can see here:

val user = User(birthYear = 1955, lastName = "Wayne", firstName = "Bruce")

Finally, since Java doesn't have named arguments, they can be only used when calling functions defined in Kotlin. Saving parameter names in the Java bytecode is possible from Java 8 and Kotlin has backward compatibility to Java 6. If you are wondering how we were able to use named arguments on a function that is defined on the string type in the Java standard library that is because this function was actually declared in the Kotlin standard library. The Kotlin standard library has numerous extension functions (we'll learn about them in the next chapter) that extend types from the Java standard library.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.13.164