Last active
May 19, 2022 12:38
-
-
Save krzyzanowskim/cfdc1990b8988469decf to your computer and use it in GitHub Desktop.
Where "where" may be used? http://blog.krzyzanowskim.com/2015/11/13/where-where-may-be-used/
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
import Cocoa | |
// for-in | |
func checkForIn(array: [Int], dict: [Int: String]) { | |
for num in array where dict[num] != nil { | |
num | |
} | |
} | |
checkForIn([1,2,3,4], dict: [1:"one", 2:"two"]) | |
// do-catch | |
enum ResponseError: ErrorType { | |
case HTTP(Int) | |
} | |
func errorProne() throws { | |
throw ResponseError.HTTP(404) | |
} | |
do { | |
try errorProne() | |
} catch ResponseError.HTTP(let code) where code >= 400 && code % 2 == 0 { | |
print("400 Bad Request") // match | |
} catch ResponseError.HTTP(let code) where code >= 500 && code < 600 { | |
print("Internal Server Error") | |
} | |
// while | |
func checkWhile(inout array: [Int]?) { | |
while let arr = array where arr.count < 5 { | |
array?.append(0) | |
} | |
} | |
var mutableArray:[Int]? = [] | |
checkWhile(&mutableArray) | |
// if | |
func checkIf(string: String?) { | |
if let str = string where str == "checkmate" { | |
print("game over") | |
return | |
} | |
print("let's play") | |
} | |
checkIf("checkmate") | |
// guard | |
func checkGuard(string: String?) { | |
guard let str = string where str != "checkmate" else { | |
print("game over") | |
return | |
} | |
print("let's play") | |
} | |
checkGuard("checkmate") | |
// switch-case | |
var value = (1,2) | |
switch value { | |
case let (x, y) where x == 1: | |
// match 1 | |
break | |
case let (x, y) where x / 5 == 1: | |
// not-match | |
break | |
default: | |
break | |
} | |
// generics | |
func genericFunction<S where S: StringLiteralConvertible>(string: S) { | |
print(string) | |
} | |
genericFunction("lambada") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@krzyzanowskim another way to write noop could look like this
default: ()