Means that the last parameter of the constructor is a lambda, that's not the case here, as you have 2 method calls : constructor followed by invoke on the Action instance.
j
Jacob
10/06/2021, 12:44 PM
The parentheses tell the compiler that the constructor invocation is complete before the {}
c
Casey Brooks
10/06/2021, 2:46 PM
For these cases, you can define factory functions on the companion object which look like a constructor, but are more flexible. For instance, define a factory that applies accepts that trailing lambda and applies it, so you can use the intended syntax of
newString
Copy code
data class StringAction(val value: String) : Action {
companion object {
operator fun invoke(value: String, f: ()->String): String {
return StringAction(value).invoke(f)
}
}
}
g
Grégory Lureau
10/06/2021, 4:51 PM
Looks a bit weird to me to have that on a data class, do you have a good use case for that? (Is it not better to use SAM interface then?)