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?
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
✗ 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?
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
✗ 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?
AAllows returning multiple unrelated types
BHides implementation details while preserving type safety
CRemoves the need for protocols
DMakes the code slower
✗ 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?
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
✗ 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?
AType erasure
BInheritance
CProtocol-oriented programming
DOptionals
✗ 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.
Practice
(1/5)
1. What does the some keyword do when used in a Swift function's return type?
easy
A. It forces the function to return a specific concrete type.
B. It hides the exact return type but guarantees it conforms to a protocol.
C. It makes the function return any type without restrictions.
D. It allows the function to return multiple different types at runtime.
Solution
Step 1: Understand the purpose of some keyword
The some keyword in Swift is used to declare an opaque return type, which means the exact type is hidden but it must conform to a specified protocol.
Step 2: Compare with other options
The other options describe behaviors that do not match the opaque type concept: forcing a concrete type, returning any type freely, or allowing multiple types at runtime.
Final Answer:
It hides the exact return type but guarantees it conforms to a protocol. -> Option B
Quick Check:
Opaque types = hidden concrete type with protocol conformance [OK]
Hint: Remember: some hides type but keeps protocol rules [OK]
Common Mistakes:
Thinking some allows any return type without constraints
Confusing some with generics
Believing some returns multiple types dynamically
2. Which of the following is the correct syntax to declare a function returning an opaque type conforming to View protocol?
easy
A. func makeView() -> some View { Text("Hello") }
B. func makeView() -> any View { Text("Hello") }
C. func makeView() -> View { Text("Hello") }
D. func makeView() -> some View() { Text("Hello") }
Solution
Step 1: Identify correct syntax for opaque return type
The correct syntax uses some followed by the protocol name without parentheses: -> some View.
Step 2: Check each option
func makeView() -> some View { Text("Hello") } matches the correct syntax. func makeView() -> any View { Text("Hello") } uses any which is for existential types, not opaque. func makeView() -> View { Text("Hello") } returns the protocol type directly, which is not allowed. func makeView() -> some View() { Text("Hello") } incorrectly adds parentheses after View.
Final Answer:
func makeView() -> some View { Text("Hello") } -> Option A
Quick Check:
Opaque type syntax = -> some Protocol [OK]
Hint: Use some without parentheses for opaque types [OK]
Common Mistakes:
Using any instead of some for opaque types
Adding parentheses after protocol name
Returning protocol type directly instead of opaque type
A. Function returns different concrete types violating opaque type rules.
B. Missing protocol conformance in structs.
C. Incorrect function return type syntax.
D. No error; code is correct.
Solution
Step 1: Understand opaque type restrictions
Functions returning some Protocol must always return the same concrete type, even if hidden.
Step 2: Analyze the function's return paths
This function returns Dog or Cat depending on input, which breaks the opaque type rule.
Final Answer:
Function returns different concrete types violating opaque type rules. -> Option A
Quick Check:
Opaque type = single hidden type only [OK]
Hint: Opaque return must be one concrete type, not multiple [OK]
Common Mistakes:
Returning different types for opaque return
Confusing opaque types with protocol types
Ignoring compiler error about inconsistent return types
5. You want to write a function that returns an opaque type conforming to Collection protocol, but the collection should be filtered to only even numbers from an input array. Which of these implementations correctly uses some keyword and returns the filtered collection?
hard
A. func evenNumbers(from numbers: [Int]) -> some Collection {
return numbers.sorted()
}
B. func evenNumbers(from numbers: [Int]) -> some Collection {
return numbers.map { $0 * 2 }
}