October 2019
Intermediate to advanced
434 pages
11h 54m
English
In Java, it's common to define helper classes/methods that all act on a common type. An example of this might be a StringUtils class that contains a number of helper methods that all act on String. In Kotlin, we could define top-level functions for this that might all take a String parameter, but we could also solve this with extension functions.
Let's first imagine a StringHelpers class in Java:
public class StringHelpers { public static void log(String msg) { System.out.println("log: " + msg); }}
We could call this from both Java and Kotlin by passing a string into each invocation. With extension functions, we can define a function that, from Kotlin, will appear to be a method on the String type itself, and, from Java, ...
Read now
Unlock full access