It seems there is not in Kotlin infix function without parameters.
I mean, we can not in some way implement something like:
fun main() {
println(1 m)
}
infix fun Number.m() = Meter(this.toDouble())
class Meter(val value: Double)
Or exist some trick?
j
Joffrey
10/20/2022, 8:28 PM
You cannot have it infix, but usually what we do is use an extension property instead:
Copy code
val Number.m: Meter
get() = Meter(this.toDouble())
And the use it like
1.m
➕ 3
r
Ruckus
10/21/2022, 2:22 AM
As a side note, infix by definition requires something on either side. What you're talking about would be a postfix (or suffix) function.
➕ 3
v
Viktor Sirotin
10/21/2022, 9:14 AM
Yes, you are right.
I just wanted to check to be on the safe side.
As far as I understand, there are no plans to implement suffix and postfix functions in Kotlin at the moment. See for example https://discuss.kotlinlang.org/t/suffix-notation/1369/9