Repository navigation
Composable functions with native C# control flow #1555
Stas Bes (ruLegen)
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I'd like to share an idea for discussion, not propose changing Reactor's current direction. I recognize that a lot of work has already gone into the existing model, and I'm not expect that it be replaced. I'd be interested to hear what you think of this authoring style.
I became interested in functional, declarative UI for .NET MAUI and started adapting ideas from Jetpack Compose, where components are ordinary composable functions, parent-child relationships follow nesting, and standard control flow such as
if,switch, and loops determines which children are composed.One possible C# API shape for this approach might look like this:
This style requires a composition context to be created and passed through each composable call. Jetpack Compose uses a Kotlin compiler plugin to generate and thread that context through the calls.
The same general approach is workable in C# with a Roslyn source generator. A source generator cannot modify the original source code, but it can emit generated counterparts of composable methods inside the partial type, re-emitting their bodies with the necessary changes. Schematically, the generated code could look like this:
A composable such as
UI.Textcan add its element to the current composition:The actual generated code can also handle parameter-change tracking, skipping unchanged calls, diagnostics, and other composition work.
The developer writes ordinary C# with standard control flow. Built-in components are hand-written composable functions whose implementation details stay hidden. When needed, users can create their own components by writing a function and marking it with [Composable].
I'd be interested to hear what you think of this approach overall.
By the way, if you'd like to give it a try playing around with it i have implemented this as a working prototype in DotNetCompose. The repo also includes TUI and MAUI samples
All reactions