Everything else is just a tool to model your domain in a way that’s easier to read and reason about, and to produce much shorter code. Interfaces define module boundaries and can be mocked to enable simpler testing. Through classes, objects, and data structures, you can model your domain entities. Enums make a list of limited options crystal clear. Extensions allow you to add functionality to existing types, without having access to their original source code, giving advantage of better logic structure. With generics, you can write universal code, promoting reuse. And overloaded operators give that extra edge to common operations that benefit from succinct, crisp syntax.
Interfaces
Description of API that you can call.
Functional interfaces
A middle ground between a single function and an interface. A special case when you may have (or expect) a more complex contract than a function, but there’s only a Single Abstract Method (SAM) to implement. Kotlin has a nice syntax sugar for such cases.
Note: Functional interfaces do not translate well to Swift/Objective-C.
fun interface Action {
fun perform()
fun then(next: Action): Action = Action {
perform()
next.perform()
}
}
fun runActionLater(action: Action) {
action.perform()
}
runActionLater(
Action { 1 }
.then { 2 }
.then { 3 }
)