Why is it necessary to wrap the `data class` with ...
# getting-started
c
Why is it necessary to wrap the
data class
with parentheses?
g
StringAction("...") {}
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
The parentheses tell the compiler that the constructor invocation is complete before the {}
c
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
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?)