0
0
Swiftprogramming~5 mins

Opaque types with some keyword in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the some keyword do in Swift when used with return types?
The some keyword tells Swift to return an opaque type, meaning the function returns a value of a specific type that conforms to a protocol, but the exact type is hidden from the caller.
Click to reveal answer
intermediate
Why use opaque types instead of protocols directly in Swift?
Opaque types let you hide the concrete type while still guaranteeing protocol conformance, enabling flexibility and abstraction without exposing implementation details.
Click to reveal answer
intermediate
Can a function with an opaque return type return different concrete types on different calls?
No. The function must always return the same concrete type, even though the caller only knows it conforms to a protocol.
Click to reveal answer
beginner
Example: What is the return type of this Swift function?<br>
func makeShape() -> some Shape { Circle() }
The return type is an opaque type conforming to Shape. The caller knows it returns some Shape, but the exact type is Circle and is hidden.
Click to reveal answer
intermediate
How do opaque types improve code safety and flexibility?
They allow hiding implementation details while ensuring the returned value meets protocol requirements, making code easier to change without breaking callers.
Click to reveal answer
What does the some keyword indicate in a Swift function's return type?
AThe function returns a type erased to <code>Any</code>
BThe function returns any type without restrictions
CThe function returns a generic type parameter
DThe function returns a specific type conforming to a protocol, but the exact type is hidden
Can a function with an opaque return type return different concrete types on different calls?
ANo, it must always return the same concrete type
BYes, it can return any type conforming to the protocol
COnly if the types share the same superclass
DOnly if the function is generic
Which of these is a benefit of using opaque types with some in Swift?
AAllows returning multiple unrelated types
BHides implementation details while preserving type safety
CRemoves the need for protocols
DMakes the code slower
What protocol conformance does a function with return type some Shape guarantee?
AThe returned value conforms to <code>Shape</code>
BThe returned value is a <code>Shape</code> instance
CThe returned value is any type
DThe returned value is a generic parameter
Which Swift feature is closely related to opaque types with some?
AType erasure
BInheritance
CProtocol-oriented programming
DOptionals
Explain what opaque types with the some keyword are in Swift and why they are useful.
Think about how <code>some</code> hides the exact type but still tells what protocol it follows.
You got /5 concepts.
    Describe the rules and limitations when using opaque types with some in function return types.
    Focus on what the caller sees versus what the function actually returns.
    You got /4 concepts.