Is it possible to add an operator-overloading extension function in Kotlin?
Asked Answered
I

1

7

I mean something like this:

fun operator Table.get(column_name: String) = this.column(column_name)
// Currently gives an error: "Expecting a top level declaration"

Table instance currently works like: table.column("column_name")

I want to make it work like this: table["column_name"]

Iridic answered 1/5, 2019 at 18:16 Comment(0)
G
9

This is possible, it's just that the operator keyword has go before the fun keyword in the declaration (as do other modifiers, such as infix, inline, etc.):

operator fun Table.get(column_name: String) = this.column(column_name)
Gifford answered 1/5, 2019 at 18:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.