I'm learning Android Studio Compose。
The Code A is from the artical Layouts in Jetpack Compose
The fun BodyContent(modifier: Modifier = Modifier) make me confuse. I check the source Code B, I find the first Modifier in fun BodyContent(modifier: Modifier = Modifier) is an interface, and the second Modifieris an object.
I don't understand why Android Studio Compose source code use the same name for both interface and object, it make me confuse, I don't know whether the compiler of Android is confused too. :)
Why does both the interface and the object use the same name in Android Studio Compose source code ? Is it a good solution?
Code A
@Composable
fun BodyContent(modifier: Modifier = Modifier) {
Row(modifier = modifier
.background(color = Color.LightGray)
.padding(16.dp)
.size(200.dp)
.horizontalScroll(rememberScrollState()),
content = {
StaggeredGrid {
for (topic in topics) {
Chip(modifier = Modifier.padding(8.dp), text = topic)
}
}
})
}
Code B
interface Modifier {
fun <R> foldIn(initial: R, operation: (R, Element) -> R): R
fun <R> foldOut(initial: R, operation: (Element, R) -> R): R
...
interface Element : Modifier {
override fun <R> foldIn(initial: R, operation: (R, Element) -> R): R =
operation(initial, this)
override fun <R> foldOut(initial: R, operation: (Element, R) -> R): R =
operation(this, initial)
override fun any(predicate: (Element) -> Boolean): Boolean = predicate(this)
override fun all(predicate: (Element) -> Boolean): Boolean = predicate(this)
}
/**
* The companion object `Modifier` is the empty, default, or starter [Modifier]
* that contains no [elements][Element]. Use it to create a new [Modifier] using
* modifier extension factory functions:
*
* @sample androidx.compose.ui.samples.ModifierUsageSample
*
* or as the default value for [Modifier] parameters:
*
* @sample androidx.compose.ui.samples.ModifierParameterSample
*/
// The companion object implements `Modifier` so that it may be used as the start of a
// modifier extension factory expression.
companion object : Modifier {
override fun <R> foldIn(initial: R, operation: (R, Element) -> R): R = initial
override fun <R> foldOut(initial: R, operation: (Element, R) -> R): R = initial
override fun any(predicate: (Element) -> Boolean): Boolean = false
override fun all(predicate: (Element) -> Boolean): Boolean = true
override infix fun then(other: Modifier): Modifier = other
override fun toString() = "Modifier"
}
}
from Why does both the Interface and the Object use the same name in Android Studio Compose source code?
No comments:
Post a Comment