0
0
Swiftprogramming~10 mins

Conditional conformance in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to make the struct conform to Equatable only when T is Equatable.

Swift
struct Box<T> {{
    var value: T
}}
extension Box: Equatable where T: [1] {
    static func == (lhs: Box<T>, rhs: Box<T>) -> Bool {
        return lhs.value == rhs.value
    }
}
Drag options to blanks, or click blank then click option'
AComparable
BCodable
CHashable
DEquatable
Attempts:
3 left
💡 Hint
Common Mistakes
Using Comparable instead of Equatable in the where clause.
Forgetting the where clause entirely.
Trying to conform without constraints.
2fill in blank
medium

Complete the code to make the generic enum conform to CustomStringConvertible only when T conforms to CustomStringConvertible.

Swift
enum Result<T> {{
    case success(T)
    case failure(Error)
}}
extension Result: CustomStringConvertible where T: [1] {
    var description: String {
        switch self {
        case .success(let value):
            return "Success: \(value.description)"
        case .failure(let error):
            return "Failure: \(error.localizedDescription)"
        }
    }
}
Drag options to blanks, or click blank then click option'
ACustomStringConvertible
BCodable
CEquatable
DHashable
Attempts:
3 left
💡 Hint
Common Mistakes
Using Codable instead of CustomStringConvertible.
Not adding the where clause.
Trying to use description without the constraint.
3fill in blank
hard

Fix the error in the code by completing the where clause to make the struct conform to Hashable only when T is Hashable.

Swift
struct Wrapper<T> {{
    var item: T
}}
extension Wrapper: Hashable where T: [1] {
    func hash(into hasher: inout Hasher) {
        hasher.combine(item)
    }
}
Drag options to blanks, or click blank then click option'
AEquatable
BCodable
CHashable
DComparable
Attempts:
3 left
💡 Hint
Common Mistakes
Using Equatable instead of Hashable in the where clause.
Omitting the where clause.
Trying to hash without T being Hashable.
4fill in blank
hard

Fill both blanks to make the generic struct conform to Codable only when T conforms to Codable.

Swift
struct Container<T> {{
    var content: T
}}
extension Container: Codable where T: [1] & [2] {
    // Codable conformance is automatic here
}
Drag options to blanks, or click blank then click option'
AEncodable
BHashable
CDecodable
DEquatable
Attempts:
3 left
💡 Hint
Common Mistakes
Using only Encodable or only Decodable.
Using unrelated protocols like Hashable or Equatable.
Not using the & operator to combine constraints.
5fill in blank
hard

Fill all three blanks to create a generic struct that conforms to Comparable only when T conforms to Comparable, and implement the < operator.

Swift
struct Pair<T> {{
    var first: T
    var second: T
}}
extension Pair: Comparable where T: [1] {
    static func < (lhs: Pair<T>, rhs: Pair<T>) -> Bool {
        if lhs.first [2] rhs.first {
            return true
        } else if lhs.first == rhs.first {
            return lhs.second [3] rhs.second
        } else {
            return false
        }
    }
}
Drag options to blanks, or click blank then click option'
AComparable
B<
C>
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < in comparisons.
Not constraining T to Comparable.
Using == instead of < for ordering.