Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"]

like image 245
Veneet Reddy Avatar asked Mar 04 '23 01:03

Veneet Reddy


1 Answers

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)
like image 135
zsmb13 Avatar answered Mar 06 '23 14:03

zsmb13