Out of curiosity, has anyone ever written a recurs...
# compose
d
Out of curiosity, has anyone ever written a recursive @Composable function?
👀 3
j
Yes
I have my own UI framework and I have a Row that has a Row in it
🧌 3
d
The Row calls itself? What's the base case?
j
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
Hmm, I'm guessing you just do this for code re-use. Otherwise I can't see any other reason.
z
d
Nice!
j
If you're implementing a TreeView, that's usually recursive.
h
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
Yes. I wanted to display a comment tree view and used DFS to render. It worked as expected
190 Views