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

karandeep singh

05/03/2020, 8:28 PM
can @Compose annotated functions return values in newer apis now?
t

Timo Drick

05/03/2020, 9:03 PM
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

karandeep singh

05/03/2020, 9:21 PM
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

Adam Powell

05/03/2020, 9:24 PM
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

Vinay Gaba

05/03/2020, 11:54 PM
Is there any sample where a composable is returning a value. I’d love to see an example!
a

Adam Powell

05/04/2020, 12:05 AM
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

Vinay Gaba

05/04/2020, 12:45 AM
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

Adam Powell

05/04/2020, 1:25 AM
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

Vinay Gaba

05/04/2020, 1:52 AM
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

Adam Powell

05/04/2020, 2:00 AM
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

Vinay Gaba

05/04/2020, 2:46 AM
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