|
|
|
|
|
by cel1ne
3325 days ago
|
|
I used extensions with multiple receivers for adding context inside data-structures. Example: I have a layout-algorithm for a grid that takes sections: interface Section {
val title: String
fun something()
}
Then I have the grid-algorithm. Inside I store expanded/collapsed state for each section. When I use a member-extension with both receivers I can write that pretty conveniently: class GridLayout(sections: List<Section>) {
/* the state */
val expandedSections = mutableSetOf<Section>()
/* the extensions I need in this context */
val Section.isExpanded get() = this@Section in expandedSections
fun Section.expand() = expandedSections += this@Section
fun compute() {
...
if (!section1.isExpanded) {
section2.expand()
}
...
}
}
|
|