liveBook · Manning

Member extension function

As explained in Kotlin in Action, 2E

A member extension function is a function declared as a member of one class while extending another receiver type.

A member extension function belongs to one class while extending another receiver type. This combines the containing class’s context with the receiver’s API, making it useful for readable internal DSLs. section 13.4.3

class Table {
    fun Column<Int>.autoIncrement(): Column<Int>    #1
    // ...
}

Exposed declares autoIncrement as a member of Table and an extension on Column<Int>, then chains it after integer("id"). section 13.4.3

The declaration gives the function two useful contexts:

  • Table supplies the containing-class context used by the SQL table DSL. section 13.4.3
  • Column<Int> becomes the extension receiver, so autoIncrement can be called directly on an integer column. section 13.4.3
  • Returning the same column enables declarations such as val id = integer("id").autoIncrement(). section 13.4.3

Table declaration

object Country : Table() { val id = integer("id").autoIncrement() val name = varchar("name", 50) override val primaryKey = PrimaryKey(id) } The declaration describes a database table; Exposed can create it through SchemaUtils.create(Country) inside a transaction.

How Kotlin in Action, 2E covers it

How deep it goes
Defines dual membership, demonstrates receiver access, and shows chaining, but stops short of broader dispatch-resolution mechanics.
Examples
Uses Exposed Country and Customer tables, column declarations, autoIncrement, joins, predicates, and generated SQL.
What it assumes
Assumes familiarity with extension functions and introduces their combination with class membership through an SQL example.
What you can do after
Apply it via Exposed to define table columns and fluent SQL constraints with access to both table and column contexts.
How it connects
Connects member extensions with extension functions, extension properties, internal DSLs, SQL tables, and database schema generation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.