Bird
Raised Fist0
Swiftprogramming~20 mins

Where clauses for complex constraints in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Where Clause Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of generic function with where clause
What is the output of this Swift code using a generic function with a where clause?
Swift
func describe<T>(value: T) where T: CustomStringConvertible, T: Comparable {
    if value > value {
        print("Impossible")
    } else {
        print("Value is \(value)")
    }
}
describe(value: 5)
ARuntime error
BImpossible
CValue is 5
DCompilation error due to where clause
Attempts:
2 left
💡 Hint
Think about the comparison and the where clause constraints.
🧠 Conceptual
intermediate
1:30remaining
Purpose of where clauses in Swift generics
What is the main purpose of using a where clause in Swift generic declarations?
ATo add constraints on generic types for more specific behavior
BTo declare variables inside a generic function
CTo create a new type alias
DTo handle errors in generic functions
Attempts:
2 left
💡 Hint
Think about how generics can be limited to certain types.
🔧 Debug
advanced
2:30remaining
Identify the error in this generic function with where clause
What error does this Swift code produce?
Swift
func compare<T, U>(a: T, b: U) where T == U, T: Comparable {
    if a < b {
        print("a is less")
    } else {
        print("a is not less")
    }
}
compare(a: 3, b: "3")
AType mismatch error at call site
BRuntime crash due to invalid cast
CCompilation error: missing protocol conformance
DNo error, prints "a is less"
Attempts:
2 left
💡 Hint
Check the types passed to the function and the where clause.
📝 Syntax
advanced
1:30remaining
Correct syntax for where clause with multiple constraints
Which option shows the correct syntax for a generic function with multiple where clause constraints?
Swift
func process<T>(item: T) where ... {
    // implementation
}
AT: Equatable, T: Hashable
BT: Equatable & Hashable
CT: Equatable, Hashable
DT: Equatable && Hashable
Attempts:
2 left
💡 Hint
Swift uses a specific symbol to combine protocol constraints in where clauses.
🚀 Application
expert
3:00remaining
Using where clause to restrict protocol extension
Given this protocol and extension, what will be the output of calling example() on an instance of MyStruct?
Swift
protocol ExampleProtocol {
    associatedtype Item
    func example()
}

extension ExampleProtocol where Item: Numeric {
    func example() {
        print("Numeric Item")
    }
}

struct MyStruct: ExampleProtocol {
    typealias Item = Int
}

let instance = MyStruct()
instance.example()
ACompilation error: missing example() implementation
BNo output
CRuntime error
DNumeric Item
Attempts:
2 left
💡 Hint
Check the protocol extension and the associated type constraint.

Practice

(1/5)
1. What is the main purpose of a where clause in Swift when used with generics?
easy
A. To define a function's return type
B. To declare a new generic type
C. To add extra conditions that generic types must satisfy
D. To create a loop over a collection

Solution

  1. Step 1: Understand generics and constraints

    Generics allow flexible types, but sometimes we want to restrict them to certain types or protocols.
  2. Step 2: Role of where clause

    The where clause adds extra conditions to generic parameters, ensuring they meet specific requirements.
  3. Final Answer:

    To add extra conditions that generic types must satisfy -> Option C
  4. Quick Check:

    Where clause = extra generic constraints [OK]
Hint: Where clauses add conditions to generics or loops [OK]
Common Mistakes:
  • Confusing where clause with function return type
  • Thinking where declares new types
  • Using where to create loops
2. Which of the following is the correct syntax for using a where clause in a generic function in Swift?
easy
A. func example<T>(value: T) where T: Equatable {}
B. func example(value: T) {}
C. func example<T>(value: T) if T: Equatable {}
D. func example<T>(value: T) where T == Equatable {}

Solution

  1. Step 1: Recall correct where clause syntax

    In Swift, the where clause comes after the generic parameter list and before the function body.
  2. Step 2: Check each option

    func example<T>(value: T) where T: Equatable {} uses correct syntax: func example<T>(value: T) where T: Equatable {}. Others misuse placement or keywords.
  3. Final Answer:

    func example<T>(value: T) where T: Equatable {} -> Option A
  4. Quick Check:

    Where clause after generics = correct syntax [OK]
Hint: Where clause always follows generic parameters () [OK]
Common Mistakes:
  • Placing where inside angle brackets
  • Using if instead of where
  • Using == instead of : for protocol conformance
3. What will be the output of the following Swift code?
func printIfEqual(_ a: T, _ b: U) where T == U, T: Equatable {
    if a == b {
        print("Equal")
    } else {
        print("Not Equal")
    }
}

printIfEqual(5, 5)
printIfEqual("hi", "hello")
medium
A. Equal\nEqual
B. Compilation error
C. Not Equal\nNot Equal
D. Equal\nNot Equal

Solution

  1. Step 1: Understand the where clause constraints

    The function requires T and U to be the same type and conform to Equatable, so both arguments must be comparable and same type.
  2. Step 2: Analyze the calls

    First call: 5 and 5 are Ints, equal, so prints "Equal". Second call: "hi" and "hello" are Strings, same type, but not equal, so prints "Not Equal".
  3. Final Answer:

    Equal Not Equal -> Option D
  4. Quick Check:

    Same type & Equatable check = output [OK]
Hint: Check type equality and Equatable conformance for output [OK]
Common Mistakes:
  • Assuming different types allowed
  • Expecting compilation error
  • Ignoring Equatable conformance
4. Identify the error in this Swift function using a where clause:
func compareValues(a: T, b: U) where T: Comparable, U: Comparable {
    if a < b {
        print("a is less")
    } else {
        print("b is less or equal")
    }
}
medium
A. Missing return type in function signature
B. Cannot compare different generic types with < operator
C. where clause syntax is incorrect
D. Function parameters should be optional

Solution

  1. Step 1: Understand the where clause constraints

    The function requires T and U to be Comparable, but they can be different types.
  2. Step 2: Check the comparison operation

    Comparing a < b is invalid because Swift cannot compare two different types even if both are Comparable.
  3. Final Answer:

    Cannot compare different generic types with < operator -> Option B
  4. Quick Check:

    Different Comparable types can't be compared directly [OK]
Hint: Comparable types must be same to compare with < [OK]
Common Mistakes:
  • Assuming Comparable allows cross-type comparison
  • Ignoring type mismatch in operators
  • Thinking where clause syntax is wrong
5. Given a generic function that filters an array of elements only if they conform to both Comparable and Numeric, which where clause correctly expresses this constraint?
func filterNumbers<T>(items: [T]) -> [T] where ... {
    return items.filter { $0 > 0 }
}
hard
A. T: Comparable & Numeric
B. T: Comparable, Numeric
C. T: Comparable where T: Numeric
D. T: Numeric or Comparable

Solution

  1. Step 1: Understand multiple protocol constraints

    To require a type to conform to multiple protocols, use the & symbol between them.
  2. Step 2: Analyze each option

    T: Comparable & Numeric correctly uses T: Comparable & Numeric. T: Comparable, Numeric uses comma which is invalid syntax. T: Comparable where T: Numeric misuses where inside where clause. T: Numeric or Comparable uses 'or' which is not valid.
  3. Final Answer:

    T: Comparable & Numeric -> Option A
  4. Quick Check:

    Use & to combine protocols in where clause [OK]
Hint: Use & to combine multiple protocol constraints [OK]
Common Mistakes:
  • Using commas instead of &
  • Misplacing where keyword
  • Using 'or' instead of &
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard