0
0
Swiftprogramming~20 mins

Opaque types with some keyword in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Opaque Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of function returning opaque type
What is the output of this Swift code using some keyword for opaque return type?
Swift
protocol Shape {
    func area() -> Double
}

struct Circle: Shape {
    var radius: Double
    func area() -> Double {
        return Double.pi * radius * radius
    }
}

func makeCircle() -> some Shape {
    return Circle(radius: 3)
}

print(Int(makeCircle().area()))
A18
B9
C28
DRuntime error
Attempts:
2 left
💡 Hint
Recall the formula for the area of a circle and how opaque types hide the concrete type but preserve behavior.
Predict Output
intermediate
2:00remaining
Opaque type with conditional return
What will be printed by this Swift code using some keyword with conditional return?
Swift
protocol Animal {
    func sound() -> String
}

struct Dog: Animal {
    func sound() -> String { "Woof" }
}

struct Cat: Animal {
    func sound() -> String { "Meow" }
}

func makePet(_ isDog: Bool) -> some Animal {
    if isDog {
        return Dog()
    } else {
        return Dog() // Note: returns Dog in both cases
    }
}

print(makePet(false).sound())
ARuntime error
BWoof
CCompile-time error
DMeow
Attempts:
2 left
💡 Hint
Remember that opaque types must always return the same concrete type, even if conditions differ.
🔧 Debug
advanced
2:00remaining
Identify the error with opaque type mismatch
What error does this Swift code produce and why?
Swift
protocol Vehicle {
    func wheels() -> Int
}

struct Bike: Vehicle {
    func wheels() -> Int { 2 }
}

struct Car: Vehicle {
    func wheels() -> Int { 4 }
}

func makeVehicle(_ useBike: Bool) -> some Vehicle {
    if useBike {
        return Bike()
    } else {
        return Car()
    }
}
ACompile-time error: Function with opaque return type must return same concrete type in all paths
BRuntime error: Cannot cast Bike to Car
CNo error, compiles and runs fine
DCompile-time error: Protocol Vehicle not conformed by Bike
Attempts:
2 left
💡 Hint
Opaque types require the same concrete type returned in every return path.
🧠 Conceptual
advanced
2:00remaining
Why use opaque types with some keyword?
Which of the following best explains the main advantage of using some keyword for opaque return types in Swift?
AIt disables type checking for the returned value
BIt forces the function to return only protocol types without any concrete implementation
CIt allows returning multiple different types from the same function dynamically
DIt hides the concrete return type while preserving type safety and allows the compiler to optimize code
Attempts:
2 left
💡 Hint
Think about abstraction and performance benefits.
Predict Output
expert
3:00remaining
Output of nested opaque types with some keyword
What is the output of this Swift code using nested opaque types with some keyword?
Swift
protocol Container {
    associatedtype Item
    func getItem() -> Item
}

struct Box<T>: Container {
    var item: T
    func getItem() -> T { item }
}

func makeBox() -> some Container {
    Box(item: 42)
}

func wrapBox() -> some Container {
    makeBox()
}

print(wrapBox().getItem())
A42
BCompile-time error: Opaque type mismatch
COptional(42)
DRuntime error: Cannot convert Box<Int> to Container
Attempts:
2 left
💡 Hint
Opaque types can be nested if the concrete type is consistent.