6

Github Implement F# API for ViewBuilders by chkn · Pull Request #37 · chkn/Xamar...

 3 years ago
source link: https://github.com/chkn/Xamarin.SwiftUI/pull/37?s=09
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Copy link

Owner

chkn commented on Nov 1, 2020

edited

Fixes #24

This enables a nice syntax for ViewBuilders in F# using computation expressions.

Example of the syntax:

open type SwiftUI.Views

let button = Button(fun () -> printfn "clicked") {
        Text("1")
        Text("2")
        Text("3")
        Text("4")
        Text("5")
        Text("6")
        Text("7")
        Text("8")
        Text("9")
        Text("10")
}

When compiled with optimization and then decompiled by ILSpy:

Button<TupleView<(Text, Text, Text, Text, Text, Text, Text, Text, Text, Text)>> button2 = new Button<TupleView<(Text, Text, Text, Text, Text, Text, Text, Text, Text, Text)>>(new $Views.button@16(this).Invoke, null);
Text item = Views.Text("1");
Text item2 = Views.Text("2");
Text item3 = Views.Text("3");
Text item4 = Views.Text("4");
Text item5 = Views.Text("5");
Text item6 = Views.Text("6");
Text item7 = Views.Text("7");
Text item8 = Views.Text("8");
Text item9 = Views.Text("9");
Text item10 = Views.Text("10");
// .. snip some unused locals ..
button2.Label = (TupleView<(Text, Text, Text, Text, Text, Text, Text, Text, Text, Text)>)(object)new TupleView<(Text, Text, Text, Text, Text, Text, Text, Text, Text, Text)>((item, item2, item3, item4, item5, item6, item7, item8, item9, item10));

See comments here for a description of the technique: https://github.com/chkn/Xamarin.SwiftUI/pull/37/files#diff-46c3c5ce9e0fa555d9f1c9eb0b89c5e4d8e320a5d50e4c9ed9450a196330ae99R1

TL;DR the CE is completely inlined and no reference to the new F#-specific assembly is emitted in release builds.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK