Skip to main content
100%

Example of how init/func behaves in protocols and protocol extensions

✓ Published0🌍 Public
MMoximillian
Last edited Apr 28, 2018
Created on Apr 28, 2018

This example demonstrates how Swift handles `init` and `func` declarations in protocols versus protocol extensions. It defines two similar protocol pairs—one where `P1` declares both members, and another where `P2` is empty and relies entirely on its extension—to show that a struct’s own implementations always take precedence over those in the extension. The code creates instances of `S1` and `S2` and calls their methods, printing "S1 init"/"S1 func" and "S2 init"/"S2 func" respectively, proving that the extension’s fallback implementations are never invoked when the struct provides its own.

AI-generated description

Similar vizzes