Hello, I have `infix fun A.function(b): X` , then...
# announcements
d
Hello, I have
infix fun A.function(b): X
, then I have
operator fun X.invoke(f: () -> Unit)
so I’m trying to call
a function b { ... }
But I get B cannot get invoked as a function, I’ve tried to use
plus
a function b +{ ... }
But I got something similar. If it use infix
infix operator fun X.invoke(f: () -> Unit)
is fine (
a function b invoke { ... }
), but cannot get rid of that explicit
invoke
If I do like
(a function b) { ... }
is also fine, but still ugly 🙂 Any solution? Thanks 🙂
r
You already found the solution (using parens). Unfortunately this is a matter of precedence, and you change precedence with parens.
👍 2
d
Ye, but that’s very ugly and make very little sense that:
a func b run { ... }
OK
a func b + { ... }
NO 😕
r
It makes perfect sense. Operator precedence is fundamental. By that logic, you can claim it makes no sense these have the same result:
3 * 6 + 4
4 + 3 * 6
Clearly the first should be 22 and the second 42, since the
+
came first.
But that's absurd because we understand the precedence of the operators.
Do you think it's ugly to have to do
(4 + 3) * 6
?
s
i wonder what a PEMDAS-style mnemonic for Kotlin order of operations would look like 😛
😆 1
d
Ok, talking about math operators it sounds different, the
+
was just a try that could be nice, it’s not supposed to be a math stuff. But why
a func b plus { ... }
is fine?
Ok, perhaps I got it 🙂
r
In Kotlin,
invoke
is an operator just like
plus
. Is your later example, you have no
invoke
. That last bit is a lambda passed in to
plus
.
d
Yup, got it. Thank you 🙂 I’ll try to declare the plus operator on
B
and handle like:
A.func(BPlus)
I might get out if it 🙂
r
It is unfortunate that the operator symbols have a different precedence that their names infix counterparts.
👍 1
d
Kinda like it 🙂 Thank you again
👍 1