Is that possible to add a new operator (not overlo...
# stdlib
g
Is that possible to add a new operator (not overload an existing but add a new one)?
🚫 2
j
with a compiler plugin maybe, easily using an ootb, I think no
🚫 1
👍 1
b
You might have some luck with compiler plugin, but even then it's unlikely. And even if you succeed, you'd still need an IDE plugin(s) to provide syntax support
👍 1
g
Oh, ok, thanks for a quick response
j
well, FIR is coming
maybe you can avoid IDE plugin soon
e
the closest thing that's available standard is
infix fun
I think even compiler plugins will have a difficult time adding new operators to the AST
z
This would probably break any other tooling you might want to use (e.g. ktlint, syntax highlighters on github, etc) as well
3
And even if you could somehow overcome all these technical challenges, and the continued maintenance that would be required for each new kotlin version, it’s going to make your code weirder to read for newcomers.
3
e
infix functions can have "creative" names if you so choose, just like all other identifiers:
Copy code
infix fun Int.`!`(other: Int): Int = when {
    this < other -> 0
    other == 0 || this == other -> 1
    else -> (this - 1 `!` other - 1) + (this - 1 `!` other)
}
List(6) { 5 `!` it } // => [1, 5, 10, 10, 5, 1]
not that I recommend it generally, but maybe there's some cases where it's useful in a DSL
1
m
Would be neat if IDE could hide backticks in certain functions so you could see something like `3 ** e`in place of `3
**
e`
d
We don't plan add ability to provide new operator conventions into language even with compiler plugin. Zach described reasons of it very well
j
kotlin 1.7 will be moving in the opposite direction with the removal of certain legal backtick options. especially
Copy code
`*`
R and Swift exist in the category of extensible operators in the top 10.