Where to be use “where”?

Pramod Kumar
Jun 1, 2017 · 2 min read

Where is more useful can be used to check whether that an associative type must confirm the certain protocol that falls after the where keyword.

It’s also useful to add and additional check in pattern matching in switch case. Finally, where can be used in case label of a switch statement, a catch statement of do statement and in the condition of if, while, guard, for-in and to define type constraints.

“If“— statement:

var webUrl: String? = "https://medium.com"if let path = webUrl where path.hasPrefix("https") {
//web url is valid, do great thing with web url
} else {
//web url is not valid, do other thing with-out web url
}

after Swift 3.0, in this scenario you have to use comma (,) at place of where.

“guard“ — statement:

var webUrl: String? = "https://medium.com"
guard let path = webUrl where path.hasPrefix("https") else {
//web url is not valid, do other thing with-out web url
}
//web url is valid, do great thing with web url

after Swift 3.0, in this scenario you have to use comma (,) at place of where.

“while“ — statement:

var numArray:[Int]? = []while let arr = numArray, arr.count < 5 {
numArray?.append(arr.count)
}
print(numArray) // [0,1,2,3,4]

after Swift 3.0, in this scenario you have to use comma (,) at place of where.

“for-in“ — statement:

var numArray: [Int] = [0,1,2,3,4,5,6,7,8,9]for val in numArray where (val % 2 == 0) {
print(val)
}
//Prints 0 2 4 6 8

“do-catch“ — statement:

enum ValidationError : Error {
case RuntimeError(Int)
}
func encryptStrign(string: String, password: String) throws {
if string.characters.count == 0 {
throw ValidationError.RuntimeError(10)
}
else if password.characters.count == 0 {
throw ValidationError.RuntimeError(11)
}
}
do {
try encryptStrign(string: "My secrete", password: "")
}
catch ValidationError.RuntimeError(let errorCode) where errorCode == 10 {
print("String is empty")
}
catch ValidationError.RuntimeError(let errorCode) where errorCode == 11 {
print("Password is empty") //Matched
}

“switch-case“ — statement:

let yetAnotherPoint = (3,-3)switch yetAnotherPoint {case let (x, y) where x == y:
print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
print("(\(x), \(y)) is on the line x == -y") //Matched
case let (x, y):
print("(\(x), \(y)) is just some arbitrary point")
}

“type constraint“ — statement:

func doSomething<S>(_ string: S) where S: ExpressibleByStringLiteral {
print(string)
}
doSomething("medium")
//Prints: medium

⚠️ Those scenario in which this isn’t mentioned that where can be replaced with comma. You’ve to use where only.

So where is very useful, can be used in various way.

Pramod Kumar

Written by

http://pramodkumar.tk/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade