ribesg
11/04/2020, 1:54 PMimport Scratch_12.MyUITableViewCell.Companion.register
class UITableView
class MyUITableViewCell {
companion object {
fun UITableView.register() {}
}
}
val tableView = UITableView()
with(tableView) {
MyUITableViewCell.register()
}
The IDE adds the import but the call to register
is still red and it says it cannot find it and suggests to import it, which does nothing as it’s already imported (example class names are relevant to the use case).ribesg
11/04/2020, 1:58 PMregister
to registerMyCell
and call tableView.registerMyCell()
but I find the way I wrote it in my post to be more clear and better typedRob Elliot
11/04/2020, 2:02 PMimport Scratch_12.MyUITableViewCell.Companion.register
class UITableView
class MyUITableViewCell {
companion object {
fun UITableView.register() {}
}
}
val tableView = UITableView()
with(tableView) {
register()
}
Rob Elliot
11/04/2020, 2:03 PMwith
block this
is the tableView
. And register
is an extension function on UITableView
, so you would normally call it as tableView.register()
- so in a with
block you call it as just register()
.ribesg
11/04/2020, 2:08 PMRob Elliot
11/04/2020, 2:13 PMclass UITableView
class MyUITableViewCell {
companion object {
fun register(tableView: UITableView) {}
}
}
class MyOtherUITableViewCell {
companion object {
fun register(tableView: UITableView) { }
}
}
val tableView = UITableView()
with(tableView) {
MyUITableViewCell.register(this)
MyOtherUITableViewCell.register(this)
}
Rob Elliot
11/04/2020, 2:19 PMfun UITableView.register()
extension functions on the companion objects of multiple Cell class companion objects, and get the compiler to disambiguate between them by prefacing the call with the Cell class name?
If so I don’t think that is possible.louiscad
11/04/2020, 2:25 PMRob Elliot
11/04/2020, 2:29 PMribesg
11/04/2020, 2:42 PMRob Elliot
11/04/2020, 2:43 PMkralli
11/04/2020, 3:05 PMMyUITableViewCell
as dispatcher and UITableView
as receiver:
class UITableView
class MyUITableViewCell {
companion object {
fun UITableView.register() {}
}
}
val tableView = UITableView()
with(MyUITableViewCell) {
tableView.register()
}
You are to judge whether or not you like this.kralli
11/04/2020, 3:16 PMfun example()
do not have a dispatcher nor receiver
• extension functions like fun Receiver.example()
have an explicit receiver but no dispatcher
• functions of classes like class Receiver { fun example() }
have an implicit receiver but no dispatcher
• extension functions in classes like class Dispatcher { fun Receiver.example() }
have both an explicit receiver as well as a dispatcher
Dispatchers always have to be bound implicitly (e.g. using with
) and receivers specified explicitly (i.e. calling the function on the object).ribesg
11/04/2020, 5:05 PM