0
0
Swiftprogramming~20 mins

Existential types (any keyword) in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Existential Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of existential type usage with 'any' keyword
What is the output of this Swift code using the 'any' keyword for existential types?
Swift
protocol Greetable {
    func greet() -> String
}

struct Person: Greetable {
    func greet() -> String {
        "Hello from Person"
    }
}

func welcome(_ greeter: any Greetable) {
    print(greeter.greet())
}

let p = Person()
welcome(p)
AProtocol 'Greetable' cannot be used as a type
BError: Missing 'any' keyword before 'Greetable'
CHello from Person
DHello from any
Attempts:
2 left
💡 Hint
Remember that 'any' keyword is used to explicitly mark existential types in Swift 5.7+.
Predict Output
intermediate
2:00remaining
Value stored in variable with 'any' existential type
What is the value of variable 'message' after running this Swift code?
Swift
protocol Describable {
    func describe() -> String
}

struct Animal: Describable {
    func describe() -> String {
        "I am an animal"
    }
}

let creature: any Describable = Animal()
let message = creature.describe()
A"I am a creature"
B"I am an animal"
CCompile-time error: protocol used without 'any'
DRuntime error: cannot call describe() on existential
Attempts:
2 left
💡 Hint
The 'any' keyword allows calling protocol methods on the existential value.
🔧 Debug
advanced
2:00remaining
Identify the error with existential type usage
What error does this Swift code produce?
Swift
protocol Runnable {
    func run()
}

func execute(task: Runnable) {
    task.run()
}

struct Job: Runnable {
    func run() {
        print("Job running")
    }
}

let job = Job()
execute(task: job)
ANo error, prints 'Job running'
BError: Missing 'any' keyword before 'Runnable'
CError: Protocol 'Runnable' can only be used as a generic constraint because it has Self or associated type requirements
DRuntime error: cannot call run() on protocol type
Attempts:
2 left
💡 Hint
Check if the protocol has associated types or Self requirements.
Predict Output
advanced
2:00remaining
Output when using 'any' with protocol with associated type
What happens when you try to compile and run this Swift code?
Swift
protocol Container {
    associatedtype Item
    func getItem() -> Item
}

struct Box: Container {
    func getItem() -> Int {
        42
    }
}

func printItem(container: any Container) {
    print(container.getItem())
}

let box = Box()
printItem(container: box)
ACompile-time error: Missing 'any' keyword before 'Container'
BPrints 42
CRuntime error: cannot call getItem() on existential
DCompile-time error: Protocol 'Container' can only be used as a generic constraint because it has Self or associated type requirements
Attempts:
2 left
💡 Hint
Protocols with associated types cannot be used as existential types directly.
🧠 Conceptual
expert
2:00remaining
Why use 'any' keyword for existential types in Swift?
Which of the following best explains the purpose of the 'any' keyword in Swift existential types?
AIt explicitly marks a protocol type as an existential to improve code clarity and future-proofing
BIt enables protocols with associated types to be used as types directly
CIt automatically converts protocol types into generic types
DIt disables protocol conformance checking at compile time
Attempts:
2 left
💡 Hint
Think about why Swift introduced the 'any' keyword in version 5.7.