February 2018
Intermediate to advanced
350 pages
7h 35m
English
We may have a requirement where we want to have an optional parameter for a function. Consider the following example:
fun Int.isGreaterThan(anotherNumber:Int):Boolean {
return this>anotherNumber
}
We want to make anotherNumber parameter optional; we want it to be 0, if it is not passed as an argument. The traditional way is to have another overloaded function without any parameters, which would call this function with 0, like the following:
fun Int.isGreaterThan(anotherNumber:Int):Boolean {
return this>anotherNumber
}
fun Int.isGreaterThan():Boolean {
return this.isGreaterThan(0)
}
However, in Kotlin, things are quite easy and straightforward and they don't require us to define the function again just to make an argument ...
Read now
Unlock full access