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.

Object types (classes)

Custom types that focus on behavior. Object types allow you to model your domain, so you can reason about it more easily. You can give things familiar names, group properties and operations, define relationships, and form as many layers of abstraction as you like. These are subjective representations of the author’s imagination. Oftentimes quite useful, as we are used to think in models of the world.

Nested classes

Classes and interfaces can be nested, to create name spaces.

Inner classes also encapsulate a reference to an instance of their outer class and have full access to its internals. To create an instance of an inner class, an instance of its outer class is required.

class OuterClass {
  class NestedClass
  interface NestedInterface

  private val secret = 42
  internal val sameName = "whatever"

  inner class InnerClass {
    val sameName = this@OuterClass.sameName

    fun whisper() = secret
  }
}

interface OuterInterface {
  class NestedClass
  interface NestedInterface
}

val nested = OuterClass.NestedClass()
val nestedViaInterface = OuterInterface.NestedClass()
val inner = OuterClass().InnerClass()

[--]