https://kotlinlang.org logo
#compose
Title
# compose
i

iamthevoid

03/21/2022, 2:01 PM
is there way to make constructor composable? I want pass through it data that calculates with composable function. It will always call in composable context. Can i do it somehow?
i

ildar.i [Android]

03/21/2022, 2:03 PM
You can create a separate method which would return your result
👆 4
👆🏼 1
a

Adam Powell

03/21/2022, 2:26 PM
Yep. If you declare a top-level factory function as
Copy code
@Composable
fun ButtonStyle(...) = ButtonStyle(...)
then you get the same call site characteristics and user expectations as a constructor, plus you're forced to keep a "real" constructor public, which ensures that you can create these objects outside of composition, aiding both aggregation as part of other state objects and testability
👍 1
j

Javier

03/21/2022, 2:43 PM
private constructor plus operator fun invoke in companion object can “simulate” invoking the constructor
👍 1
a

Adam Powell

03/21/2022, 6:48 PM
Indeed, but then you have a private constructor and a type that can only be created during composition. 🙂
👍 2
13 Views