I was doing Using State in Compose "codelab" (Andr...
# getting-started
s
I was doing Using State in Compose "codelab" (Android) there was a code
Copy code
TodoItemInlineEditor(
    item = currentlyEditing,
    onEditItemChange = onEditItemChange,
    onEditDone = onEditDone,
    onRemoveItem = { onRemoveItem(todo) }
)
In my case I wrote in this way:
Copy code
TodoItemInlineEditor(
    item = currentlyEditing,
    onEditItemChange = {onEditItemChange},           ............................difference 1 writing inside {}
    onEditDone = {onEditDone},                       ............................difference 2 writing inside {}
    onRemoveItem = { onRemoveItem(todo) }
)
by using onEditItemChange and onEditDone inside {} somehow made my app to stop working. So what is the difference by writing inside {} and without {} ? onEditItemChange , onEditDone and onRemoveItem(todo) are lamda callback from:
Copy code
@Composable
fun TodoScreen(
    items: List<TodoItem>,
    currentlyEditing: TodoItem?,
    onAddItem: (TodoItem) -> Unit,
    onRemoveItem: (TodoItem) -> Unit,
    onStartEdit: (TodoItem) -> Unit,
    onEditItemChange: (TodoItem) -> Unit,
    onEditDone: () -> Unit
) {
.....
.....
TodoItemInlineEditor(
    item = currentlyEditing,
    onEditItemChange = onEditItemChange,
    onEditDone = onEditDone,
    onRemoveItem = { onRemoveItem(todo) }
)
....
....
}
r
a) next time you probably want to ask smth like this in #compose but b) this one is easy without knowing compose so: the difference is, that
{}
create a lambda - a new function you have
onEditItemChange: (TodoItem) -> Unit
- function which takes
TodoItem
and returns
Unit
then a
onEditItemChange = {onEditItemChange}
passes a function as argument, and the function takes 1 implicit parameter
it
(probably of type
TodoItem
I assume), and then returns a
onEditItemChange
- a function of
(TodoItem) -> Unit
So whole
{}
expression would have type
(TodoItem) -> ( (TodoItem) -> Unit) )
so either without
{}
(passing the function directly) or you need to actually call the function inside the lambda:
onEditItemChange = { onEditItemChange(it) }
4