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?✗ Incorrect
The
some keyword means the function returns a specific, but hidden, type conforming to a protocol.Can a function with an opaque return type return different concrete types on different calls?
✗ Incorrect
Opaque types require the function to return the same concrete type every time.
Which of these is a benefit of using opaque types with
some in Swift?✗ Incorrect
Opaque types hide the concrete type but keep the compiler aware of the exact type for safety.
What protocol conformance does a function with return type
some Shape guarantee?✗ Incorrect
The
some Shape return type guarantees the returned value conforms to the Shape protocol.Which Swift feature is closely related to opaque types with
some?✗ Incorrect
Opaque types work well with protocol-oriented programming by hiding concrete types but enforcing protocol conformance.
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.