Skip to content

Instantly share code, notes, and snippets.

@Geri-Borbas
Created June 26, 2020 01:56
Show Gist options
  • Select an option

  • Save Geri-Borbas/af76af9d1b4f94e9e3f0268cfacf5cc7 to your computer and use it in GitHub Desktop.

Select an option

Save Geri-Borbas/af76af9d1b4f94e9e3f0268cfacf5cc7 to your computer and use it in GitHub Desktop.
SwiftUI View struct experiments with explicit types to see DSL dissected.
import SwiftUI
let hello = "Hello"
let world = "world!"
struct ContentView_standard: View
{
var body: some View
{
HStack
{
Text(hello)
Text(world).bold().padding(-5)
}
}
}
struct ContentView_using_TupleView: View
{
var body: HStack<TupleView<(Text, ModifiedContent<Text, _PaddingLayout>)>>
{
let text: Text =
Text(world).bold()
let modifiedContent: ModifiedContent<Text, _PaddingLayout> =
text.padding(-5) as! ModifiedContent<Text, _PaddingLayout>
let tupleView: TupleView<(Text, ModifiedContent<Text, _PaddingLayout>)> =
TupleView((Text(hello), modifiedContent))
let hStack: HStack<TupleView<(Text, ModifiedContent<Text, _PaddingLayout>)>> = HStack(content: { return tupleView })
return hStack
}
}
struct ContentView_using_ViewBuilder: View
{
var body: HStack<TupleView<(Text, ModifiedContent<Text, _PaddingLayout>)>>
{
let text: Text =
Text(world).bold()
let modifiedContent: ModifiedContent<Text, _PaddingLayout> =
text.padding(-5) as! ModifiedContent<Text, _PaddingLayout>
let tupleViewViewBuilder: TupleView<(Text, ModifiedContent<Text, _PaddingLayout>)> = ViewBuilder.buildBlock(
Text(hello),
modifiedContent
)
let hStack: HStack<TupleView<(Text, ModifiedContent<Text, _PaddingLayout>)>> =
HStack(content: { return tupleViewViewBuilder })
return hStack
}
}
struct ContentView_Previews: PreviewProvider
{
static var previews: some View
{ return ContentView_using_TupleView() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment