obobo
03/01/2019, 10:03 PMfun MyObject.get(str: String): String {
return get(str) {}
}
class Scope {
operator fun Any.get(str: String): Int = 1
}
// within method body
val myObject = MyObject()
val scope = Scope()
with(scope) {
myObject.get("abc") // calls Scope's get()
}
How do I tell the compiler to call MyObject.get()? Suppose that both extension functions are define din libraries, so I can't easily change those names.obobo
03/01/2019, 10:16 PMwith(scope) {
with(this@methodClass) {
myObject.get("abc") // calls MyObject.get()
}
}
But it doesn't seem great, and won't work if I don't have a this.Czar
03/01/2019, 10:20 PMMyObject.get
I don't understand what you want to achieve there.obobo
03/01/2019, 10:25 PMCzar
03/01/2019, 10:26 PMfun MyObject.get(str: String): String {
println("MyObject extension get: $str")
return str
}
You can easily avoid the problem you describe:
import com.example.MyObject
import com.example.Scope
import com.example.get as myGet
// within method body
val myObject = MyObject()
val scope = Scope()
with(scope) {
myObject.myGet("abc") // prints "MyObject extension get: abc"
}
obobo
03/01/2019, 10:28 PMCzar
03/01/2019, 10:28 PMobobo
03/01/2019, 10:29 PM