Kelvin Chung
08/10/2025, 6:23 PMinterface Addition<T> {
fun add(lhs: T, rhs: T): T
}
interface AdditionOp<T> {
context(addition: Addition<T>)
operator fun add(rhs: T): T = addition(this, rhs)
}
or are operator overloads locked to specific signatures and AdditionOp.add()
is not possible?Nicolai C.
08/10/2025, 7:01 PMplus
not add
. I have quite a few context-aware operator functions across a handful of my DSLs.Nicolai C.
08/10/2025, 7:03 PMphldavies
08/10/2025, 7:04 PMKelvin Chung
08/10/2025, 9:50 PMobject FooAddition : Addition<Foo>
interface AdditionOp<T> {
context(addition: Addition<T>)
operator fun plus(rhs: T): T = ...
}
class Foo : AdditionOp<Foo>
Given that FooAddition
is a globally available singleton, would it be possible to invoke the +
operator "context-free"? ie. without
context(FooAddition) { ... }
I imagine that this could be useful in certain situations involving value classes.