Almost same topic as above. When having nullable s...
# compose
j
Almost same topic as above. When having nullable slot API in composable lambdas. I wish I could do:
Copy code
Component(
   slot = { ComposeBlock }.takeIf {condition}
)
I think compiler limitation want to cast from slot type being (@Composable () -> Unit)? But because nullable its complicated. Is there any nice way telling lambda is composable without need to do if else block?
e
what's wrong with if-else?
Copy code
Component(
    slot = { if (condition) { ComposeBlock } }
)
e
OP, I don’t think you should do what you wished you could do, That creates a throwaway lambda even when its not needed. Please don’t do that. That said, You can always add the composable annotation to a lambda
@Composable { ... }
l
What's preventing you from using clauses? IMHO clauses are more intuitive than some extension shenanigans.
Copy code
Component(
    slot = if (condition) {
        @Composable { /* ... */ }
    } else {
        null
    }
)
j
@ephemient Because I dont want any compose block in my component if null. Otherwise get composable block empty but render slot.
@efemoney what you mean throwaway lambda? Also adding @Composable in front doesnt work, not sure why. I think because nullable.
@Loney Chou Nothing, just makes code ugly and long. When having multiple slots code lines get very long.
l
If you do that, even if the condition is false, the lamdba object will be created. But with clauses this can be avoided.
Copy code
Component(
    slot = (@Composable {
        /* ... */
    }).takeIf(condition)
)
This is way uglier I'd say.
e
if (xxx) null else ({lambda}) only creates the lambda with the right condition. What you want will always create the lambda.
j
Right, I blame Compose syntax. No good way doing this. Ideally would be check if empty lambda in component and dont render it. But in Row/Column with spacedBy they get extra padding with empty lambdas.
@Loney Chou Yeah make sense when put it like that :)