Kotlin only allows overriding a particular set of statements, and you cannot change the list of available statements,
You must take care to redefine the operators that you are trying to stay in the spirit of the original operator, or other common uses of the mathematical symbol. But sometimes a typical character is not available. For example, setting Union ∪ can be easily done like + , because conceptually it makes sense, and this is already the built-in Set<T>.plus() operator already provided by Kotlin, or you can get creative and use the infix function for this case:
// already provided by Kotlin: // operator fun <T> Set<T>.plus(elements: Iterable<T>): Set<T> // and now add my new one, lower case 'u' is pretty similar to math symbol ∪ infix fun <T> Set<T>.u(elements: Set<T>): Set<T> = this.plus(elements) // and therefore use any of... val union1 = setOf(1,2,5) u setOf(3,6) val union2 = setOf(1,2,5) + setOf(3,6) val union3 = setOf(1,2,5) plus setOf(3,6)
Or maybe this is more clear:
infix fun <T> Set<T>.union(elements: Set<T>): Set<T> = this.plus(elements)
And continuing our list of Set statements, the intersection is a ∩ symbol, so if every programmer has a font where the letter 'n' looks ∩ , we could get away with:
infix fun <T> Set<T>.n(elements: Set<T>): Set<T> = this.intersect(elements)
or using operator overload * as:
operator fun <T> Set<T>.times(elements: Set<T>): Set<T> = this.intersect(elements)
Although you can already use the existing standard library infix function intersect() as:
val intersect = setOf(1,3,5) intersect setOf(3,5)
In those cases when you invent something new, you need to choose the nearest operator or function name. For example, denying a set of enumerations, perhaps use the operator - ( unaryMinus() ) or the operator ! ( not() ):
enum class Things { ONE, TWO, THREE, FOUR, FIVE } operator fun Set<Things>.unaryMinus() = Things.values().toSet().minus(this) operator fun Set<Things>.not() = Things.values().toSet().minus(this)
Be careful, as overloading the operator can be very useful, or it can lead to confusion and chaos. You must decide which is best while maintaining the readability of the code. Sometimes an operator is best suited if it matches the norm for that character, or replacing an infix that looks like the original character, or using a descriptive word so that there is no chance of confusion.
Always check the Kotlin Stdlib API Reference , because many of the statements you want can already be defined or have equivalent extension functions.
One more thing ...
And about your $$ statement, technically you can do it like:
infix fun String.`$$`(other: String) = "$this !!whatever!! $other"
But since you need to avoid the function name, it will be ugly to call:
val text = "you should do" `$$` "you want"
This is not an overload of the operating system and will only work if it is a function that infix can do .