0
0
Swiftprogramming~10 mins

Existential types (any keyword) in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Existential types (any keyword)
Define Protocol
Declare variable with 'any Protocol'
Assign instance conforming to Protocol
Use variable to call protocol methods
Runtime dispatch to actual implementation
This flow shows how a variable declared with 'any' holds any instance conforming to a protocol and calls its methods at runtime.
Execution Sample
Swift
protocol Greetable {
    func greet() -> String
}

struct Person: Greetable {
    var name: String
    func greet() -> String {
        return "Hello, \(name)!"
    }
}

var greeter: any Greetable

greeter = Person(name: "Alice")
print(greeter.greet())
This code declares a protocol, a variable with 'any' existential type, assigns a conforming instance, and calls a method.
Execution Table
StepActionVariable StateOutput
1Define protocol Greetable with greet()No variables yet
2Declare variable 'greeter' of type 'any Greetable'greeter = uninitialized
3Assign 'Person(name: \"Alice\")' to 'greeter'greeter = Person(name: "Alice")
4Call 'greeter.greet()'greeter = Person(name: "Alice")"Hello, Alice!" printed
5End of programgreeter = Person(name: "Alice")Program ends
💡 Program ends after printing greeting from the assigned instance.
Variable Tracker
VariableStartAfter AssignmentFinal
greeteruninitializedPerson(name: "Alice")Person(name: "Alice")
Key Moments - 2 Insights
Why do we write 'any Greetable' instead of just 'Greetable'?
In Swift 5.7+, 'any' explicitly marks existential types. It tells the compiler this variable can hold any instance conforming to the protocol, not a specific type. See execution_table step 2 and 3.
How does 'greeter.greet()' know which implementation to call?
At runtime, Swift uses dynamic dispatch to call the actual method of the instance stored in 'greeter'. This is shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the type stored in 'greeter'?
AUninitialized variable
BProtocol Greetable itself
CPerson instance with name "Alice"
DString value "Alice"
💡 Hint
Check the 'Variable State' column at step 3 in execution_table.
At which step does the program print the greeting?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the 'Output' column mentioning printed text.
If we remove 'any' keyword from the variable declaration, what happens?
AThe code compiles and works the same
BCompiler error: protocol used as a type without 'any'
CRuntime error when assigning instance
DVariable becomes a concrete type
💡 Hint
Refer to key_moments about the purpose of 'any' keyword.
Concept Snapshot
Existential types hold any instance conforming to a protocol.
Use 'any ProtocolName' to declare such variables.
Allows dynamic dispatch of protocol methods.
Example: var x: any Greetable = Person()
Use to work with heterogeneous types safely.
Full Transcript
This visual execution shows how Swift's 'any' keyword marks existential types. First, a protocol Greetable is defined with a greet() method. Then a variable 'greeter' is declared as 'any Greetable', meaning it can hold any instance conforming to Greetable. Next, an instance of Person named Alice is assigned to 'greeter'. When calling greeter.greet(), Swift dynamically calls the Person's greet() method, printing "Hello, Alice!". The 'any' keyword is required to tell the compiler this is an existential type, enabling dynamic dispatch. Removing 'any' causes a compile error. This trace helps understand how existential types work step-by-step.