Skip to content

Instantly share code, notes, and snippets.

@harlanhaskins
Created January 8, 2016 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harlanhaskins/2279e8eed15dbf63b2d2 to your computer and use it in GitHub Desktop.
Save harlanhaskins/2279e8eed15dbf63b2d2 to your computer and use it in GitHub Desktop.
An Extensible Goto framework for Swift
struct Goto {
typealias Closure = () -> ()
var closures = [String: Closure]()
func to(label: String) {
guard let closure = closures[label] else {
fatalError("Unknown label: \(label)")
}
closure()
}
mutating func set(label: String, closure: () -> ()) {
closures[label] = closure
}
}
var go = Goto()
var val = 0
go.set("cond") {
go.to(val > 10 ? "end" : "body")
}
go.set("body") {
print(val)
val += 1
go.to("cond")
}
go.set("end") {}
go.to("cond")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment