Skip to main content
Journey Uncommon Logo
JourneyUncommon
mediumSwiftgenerics and constraintsSingle-choice MCQ

What does this print, and why, given Swift's rule about static (compile-time) vs dynamic dispatch for generic context?

protocol P { func who() -> String } extension P { func who() -> String { "P" } } struct S: P { func who() -> String { "S" } } func callViaGeneric<T: P>(_ x: T) -> String { x.who() } func callViaExistential(_ x: P) -> String { x.who() } print(callViaGeneric(S()), callViaExistential(S()))