Last active
July 1, 2017 05:15
-
-
Save sidepelican/467ae186219f448fb51ca1e4daea9505 to your computer and use it in GitHub Desktop.
Swiftでkotlin風if式
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
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