I understand its convention, but nested apply means that if I want something logical like (if we use React as an example):
using kotlin:
props.apply {
// need to use first name, last name in this block only
state.apply {
// state use in here
// what if name clash? not as graceful
hasSeenToast = true
// not clear scope of this variable, is it part of state? is it part of class? or props?
}
}
vs ES6 style syntax:
val {firstName, lastName} = this.props
val { isDialogOpen, userHasSeenMessage } = this.state
hasSeenToast = true // clear it comes from class
We can end up with multiple nested apply blocks which is not graceful and could lead to ugly code. Using ES6 style syntax means we keep our code more flat and its perfectly readable. theres no question where these variables come from. In an apply block you might be accessing fields that are within the scope of the apply{} or the scope of the outer block. Its less clear in that sense. Also in the scope of the ES6 style, we can apply different names safely:
val { first_name = firstName, last_name = lastName } = this.props
// first_name, last_name accessible as local vars