https://kotlinlang.org logo
Title
r

rook

03/25/2019, 4:13 PM
Is there a way of writing
infix
functions that allows the left side to be implied by the context? For example, in
apply
🇳🇴 1
d

Dias

03/25/2019, 4:26 PM
you mean something like:
infix fun <T: Any>  T.test(other: Any) {

}
?
r

Ruckus

03/25/2019, 4:28 PM
If you're looking for something like:
infix fun String.test(other: String) {
    ...
}
"A".apply {
    test "B"
}
Then no, it's not possible.
a

Alowaniak

03/25/2019, 4:37 PM
if the argument is a lambda then it would be sort of like an infix however
r

rook

03/25/2019, 4:58 PM
@Ruckus yeah, that’s exactly what I’m talking about
c

cho

03/26/2019, 12:35 AM
@rook this works for me:
infix fun String.isSameAs(other: String): Boolean = (this == other).also { println(it) }
"SomeString".apply {
    isSameAs("Foo")
    isSameAs(this)
}
prints
false
then
true