Instantly share code, notes, and snippets.

Embed
What would you like to do?
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