Question about lambda arguments for events. If I h...
# compose
c
Question about lambda arguments for events. If I have `
Copy code
@Composable
fun MyComposable(changed: () -> Unit)`
and I want to invoke the lambda passed in during a click would I do? 1. Modifier.clickable { changed() } 2. Modifier.clickable { changed } 3. Modifier.clickable { changed.invoke() } Number 1 and 3 seem to work, but I feel like I can never remember what to use and when to use it. Any tips?
h
Number 1 and 3 are the same. Number 3 is useful when your lambda is also nullable so you can do
changed?.invoke()
which is not a good practice imo but possible. Number 2 is kind of nothing. It is essentially equivalent to
modifier.clickable { 4 }
. Ofc it might have side effects if
changed
has a custom getter but it isn't in your situation.
80% of the time I go for #3 because it is more explicit about what it is doing. Sometimes I summon the javascript developer inside and go for #1.
a
I am doing
Modifier.clickable(changed)
3
c
Oh. So that's why #2 is wrong. It should be parens not braces?
a
Yes, #2 is incorrect
c
Hm. This doesn't seem to work either.
d
try
.clickable(onClick = changed)
c
That worked! @Arkadii Ivanov are you sure that you've been able to write
Modifier.clickable(changed)
? If so then I will file a bug that in my scenario it's not working apparently.
a
That is correct, named argument should be used.