Does anyone know how to call an extension function that is also an instance of a type? For example: ...
m
Does anyone know how to call an extension function that is also an instance of a type? For example:
Copy code
fun test() {
    val bar = Bar()
    val foo = Foo()
    bar.doSomething() // How do you call this function
}

class Foo {
    fun Bar.doSomething() {
        // ...
    }
}

class Bar {
    // ...
}
m
Copy code
foo.apply {
    bar.doSomething() // How do you call this function
  }
m
I see! So the trick is that you have to be encapsulated within the instance?
m
I guess so, not sure 😅
Maybe you'd be interested in context receiver stoo
Note it doesn't have anything to do with #kotlin-native
Maybe more #general-advice next time
m
Maybe you'd be interested in context receivers too
I've read up a bit on them, but more couldn't hurt. Thanks!
Note it doesn't have anything to do with #kotlin-native
Maybe more #general-advice next time
Ahh I didn't know which channel fit the best. I will be joining that one for sure
Appreciate it
l
foo.apply
typically domes with the implication that you’re modifying foo. If you just need foo as a context,
with(foo)
is more clear.
m
Good point thanks for the addition