An Extensible Goto framework for Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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