Languages without support for default arguments often use overloading to imitate that feature.

The term overload refers to the name of a function: You use the same name (“overload” that name) for different functions as long as the argument lists differ. Here, we overload the member function f():

// Overloading/Overloading.kt import atomictest.eq class Overloading { fun f() = 0 fun f(n: Int) = n + 2 } fun main(args: Array<String>) { val o = Overloading() o.f() eq 0 o.f(11) eq 13 }

In Overloading, you see two functions with the same name, f(). The function’s signature consists of the name, argument list and return type. Kotlin distinguishes one function from another by comparing signatures. When overloading functions, the argument lists must be unique—you cannot overload on return types.

End of sample. See AtomicKotlin.com for full early-access book.

Previous          Next

©2018 Mindview LLC. All Rights Reserved.