Skip to content

Instantly share code, notes, and snippets.

@sidepelican
Last active July 1, 2017 05:15
Show Gist options
  • Save sidepelican/467ae186219f448fb51ca1e4daea9505 to your computer and use it in GitHub Desktop.
Save sidepelican/467ae186219f448fb51ca1e4daea9505 to your computer and use it in GitHub Desktop.
Swiftでkotlin風if式
enum elseer<T> {
case `true`(T)
case `false`
func `else` (falseClosure: ()->(T)) -> T {
switch self {
case .true(let result):
return result
case .false:
return falseClosure()
}
}
}
func letif<T>(_ cond: Bool, trueClosure: ()->(T)) -> elseer<T> {
if cond {
return elseer.true(trueClosure())
} else {
return elseer.false
}
}
let boolean = false
let text: String = letif (boolean) {
return "true!"
}.else {
return "false!"
}
print(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment