Overview - Existential types (any keyword)
What is it?
Existential types in Swift let you work with values of any type that conforms to a certain protocol, without specifying the exact type. The 'any' keyword explicitly marks a type as existential, meaning it can hold any value that fits the protocol. This helps write flexible code that can handle different types uniformly. It is a way to say, "I don't care what exact type this is, as long as it follows these rules."
Why it matters
Without existential types, you would need to know the exact type of every value you work with, which limits flexibility and code reuse. Existentials let you write functions and data structures that can handle many types through a common interface, making your code more adaptable and easier to maintain. Without them, programs would be more rigid and repetitive, forcing you to duplicate code for each type.
Where it fits
Before learning existential types, you should understand Swift protocols and basic type system concepts. After mastering existentials, you can explore advanced topics like opaque types, generics, and protocol-oriented programming to write even more powerful and type-safe code.