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

Dominaezzz

02/06/2021, 5:16 PM
Out of curiosity, has anyone ever written a recursive @Composable function?
👀 3
j

jw

02/06/2021, 5:35 PM
Yes
I have my own UI framework and I have a Row that has a Row in it
🧌 3
d

Dominaezzz

02/06/2021, 5:37 PM
The Row calls itself? What's the base case?
j

jw

02/06/2021, 5:39 PM
It's based on the presence of non-null arguments. So if an argument is non-null it recursively emits a Row to hold the thing represented by the arguments and then the contents of the child lambda
The recursive call always passes null as that argument so it can never recurse more than once
It's no different than calling
Row { Argument(); Row { children() } }
yourself
d

Dominaezzz

02/06/2021, 5:43 PM
Hmm, I'm guessing you just do this for code re-use. Otherwise I can't see any other reason.
z

Zach Klippenstein (he/him) [MOD]

02/06/2021, 5:59 PM
d

Dominaezzz

02/06/2021, 6:01 PM
Nice!
j

jim

02/06/2021, 7:37 PM
If you're implementing a TreeView, that's usually recursive.
h

Halil Ozercan

02/06/2021, 9:20 PM
With a limited compose experience, I too had a chance to write a recursive composable 🙂 https://github.com/zach-klippenstein/compose-richtext/blob/main/richtext-commonmark/src/main/java/com/zachklipp/richtext/markdown/Markdown.kt#L[…]95 It might not look like a recursive function at first glance due to way too many terminal conditions but it calls
visitChildren
when it doesn't terminate, which in turn calls
RecursiveRenderMarkdownAst
for all available children for the currently visited node.
j

Jeremy

02/08/2021, 6:31 PM
Yes. I wanted to display a comment tree view and used DFS to render. It worked as expected
42 Views