October 2019
Intermediate to advanced
434 pages
11h 54m
English
Similarly to C# or Swift, Kotlin supports extension functions. Extension functions allow you to extend the behavior of an existing type, even if you don't own that type. This can be extremely useful when working with cumbersome APIs or when building custom DSLs. Extension functions can be defined in any Kotlin file, but require special syntax.
The following snippet is taken from the Kotlin Standard library and demonstrates how extension functions can be used to extend common classes as well:
// forEach is defined as an extension function CharSequenceinline fun CharSequence.forEach(action: (Char) -> Unit)
The forEach function does not exist as a method on CharSequence but can be invoked as if it were a method. This not ...
Read now
Unlock full access