Pick the fix
You need largest for an array of any comparable element type. Which signature?
Hint 1 of 1
Which option keeps one concrete element type?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A func largest<T: Comparable>(_ xs: [T]) -> T?
Correct. One implementation, type-safe, specialized per type.
Why not the others?
- B
func largest(_ xs: [Any]) -> Any?. Any can't be compared without casting. - C
func largest(_ xs: [any Comparable]) -> (any Comparable)?. Two existentials may hold different types, so you can't compare them directly. - D Separate overloads for Int, Double and String. Duplicated code that misses every other Comparable type.