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

efemoney

01/13/2022, 4:31 PM
How can I require only a specific set of composable functions be called inside the scope of one of my slots. My use case is I have an “action bar” slot that can contain any number of
ActionItem(...)
composables up until a maximum number (eg based on available space, only 3
ActionItem(...)
s allowed). Is this enforceable at compile time? (eg something like coroutines’
@RestrictedSuspension
) 🤔 or can it only be implemented as a custom
Layout
(that maybe checks, at runtime, for a particular
parentData
type)?
c

Casey Brooks

01/13/2022, 5:34 PM
Requiring specific methods is kind-of against the philosophy of compose. A better solution is to use a "scope object" as the slot lambda's receiver to expose and encourage the correct APIs for that scope, while giving users the flexibility to do something different if they need to. This is done commonly throughout the Compose/Material stdlibs for reference (for example,
Column()
->
ColumnScope
,
Box()
->
BoxScope
, etc)
3
Alternatively, if it does truly need to be restricted, don't use a slot. Pass configuration parameters or a builder object to the function instead
1