can @Compose annotated functions return values in ...
# compose
k
can @Compose annotated functions return values in newer apis now?
t
I think it is possible since at least dev03. Because i used it since than. What do you want to do?
Or no i think in the old apis you used so called effects and now you can just use @Composable function instead
k
I wanted to know whether api has changed or not because during all talks they mentioned its by api design that they want to return unit.
a
Nah we unified effects and composable functions, you can return values. Generally you won't want a composable function to both emit UI and return a value though; generally if you feel a need to do that you want to use some sort of hoisted state object passed into a UI-emitting composable function instead of having it return something.
👍 1
v
Is there any sample where a composable is returning a value. I’d love to see an example!
a
remember {}
,
state {}
,
.current
on ambients, a bunch of factory methods in the API that read ambients/internally
remember {}
implementation details, all of the `.observeAsState()`/`.subscribeAsState()`/`.collectAsState()` methods for LiveData/RxJava/Flow, respectively...
v
blows my mind that I didn’t notice that
remember
was returning a value 😮 Does compose assume that if it’s not returning a value then its meant to emit UI and if it returns a value then its not meant for UI ?
a
Not necessarily. Consider
onCommit { onDispose {} }
constructs which neither return a value nor emit UI
or something like ambient
Providers(...) { ... }
which configures a sort of context for further composition, but doesn't emit UI or return a value either.
v
interesting! I’m guessing that the presence of a Layout/LayoutNode/etc type primitive inside a composable causes something to end up on the screen vs just help in the composition?
a
yes, emitting a node into the tree is an instruction to the composer and generally happens as part of the implementation details of calls to things like
Layout
but if you poke around the source for where
LayoutNode
emits happen you can see how it works; it shares a syntax with a function call but the named parameters can set `var`s on a pre-existing tree node object
v
thank you so much Adam for your consistently thoughtful responses! This really helps the early adopters in understanding the system better. Hopefully we can multiply this and share it with more folks who start learning compose!
👍 1
1