Hi all, does anyone has any idea what is the diffe...
# compose
v
Hi all, does anyone has any idea what is the difference between "returning a modifier using
then()
" and "returning a modifier directly" while creating a custom modifier?
s
AFAIK, these are the same:
Copy code
myModifier.foo()
Copy code
myModifier then Modifier.foo()
Where
then
shines is if you conditionally apply modifiers, for instance:
Copy code
val otherModifier = if (expression) {
  Modifier.foo()
} else {
  Modifier.bar()
}

val fullModifier = myModifier then otherModifier
l
For the code below:
Copy code
fun Modifier.foo(): Modifier = object : Modifier.Element {}

fun Modifier.bar(): Modifier = this then object : Modifier.Element {}
By calling
modifier.foo()
will it remove all the modifiers that
modifier
has and remain that anonymous object, while by calling
modifier.bar()
will it create a
CombinedModifier
which retain other modifiers.
m
I don't recommend ignoring the source modifier. It's counter intuitive to what the user is expecting. And on top of that lint will yell at you if you don't at least reference the
this
when creating a modifier through an extension function like above. You could in theory turn off that off for lint, but it's there for a reason.