Skip to content

Instantly share code, notes, and snippets.

@ole
Created September 9, 2019 15:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ole/917b7d4570de534efd3fc974009c635c to your computer and use it in GitHub Desktop.
Save ole/917b7d4570de534efd3fc974009c635c to your computer and use it in GitHub Desktop.
A replacement for let bindings in Swift function builders
import SwiftUI
func `let`<Value, Return>(_ expression: Value, body: (Value) -> Return) -> Return {
body(expression)
}
struct ContentView: View {
var body: some View {
GeometryReader { geometry in
`let`(geometry.size.width / 2) { lineWidth in
VStack {
Rectangle().fill(Color.red).frame(width: lineWidth)
Rectangle().fill(Color.green).frame(width: lineWidth)
Rectangle().fill(Color.blue).frame(width: lineWidth)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment