Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The extension makes Box conform to Equatable only if T itself conforms to Equatable.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Codable instead of CustomStringConvertible.
Not adding the where clause.
Trying to use description without the constraint.
✗ Incorrect
The enum Result conforms to CustomStringConvertible only if T does, so it can use value.description safely.
3fill in blank
hardFix 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'
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.
✗ Incorrect
To conform to Hashable, the generic type T must also conform to Hashable.
4fill in blank
hardFill 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'
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.
✗ Incorrect
Codable is a typealias for Encodable & Decodable, so both constraints are needed for conditional conformance.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < in comparisons.
Not constraining T to Comparable.
Using == instead of < for ordering.
✗ Incorrect
The struct conforms to Comparable only if T does. The < operator compares first values, then second values if first are equal.