Types

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Types

This page draws heavily from these swift.org references:

Protocol: Nominal or Non-Nominal?

If a type is either nominal or non-nominal, where do protocols fit? You certainly name a protocol, but can you create an instance using MyProtocol()? Well, according to Nominal Types, protocols are non-nominal:

Boxed Protocol (Existential) & Opaque Types

Boxed protocol and opaque types are two ways to hide details about a value’s concrete type. A boxed protocol type is also called an existential type, which comes from the phrase “there exists a type T such that T conforms to the protocol”.

Self & Any Types

Simulating Abstract Classes

This example is from John Sundell’s Abstract types and methods in Swift. It discusses how to simulate an abstract class, with trade-offs when using a protocol with associated type vs. composing a class and a protocol.

See forum comments
Download course materials from Github
Previous: Type Erasure Next: Protocols - Conclusion