📖 Scenario: Imagine you are building a simple app that shows different shapes. Each shape can tell its area, but you want to hide the exact type of shape from the user of your code. This helps keep your code clean and flexible.
🎯 Goal: You will create a function that returns a shape using Swift's some keyword to hide the exact type. Then you will print the area of the shape.
📋 What You'll Learn
Create a protocol called Shape with a property area of type Double
Create a struct called Circle that conforms to Shape and has a radius property
Create a function called makeCircle that returns some Shape and returns a Circle
Print the area of the shape returned by makeCircle
💡 Why This Matters
🌍 Real World
Opaque types are useful when you want to hide the exact type of a value but still guarantee it follows a certain interface. This is common in UI frameworks and libraries where you want to return views or shapes without exposing their concrete types.
💼 Career
Understanding opaque types and the <code>some</code> keyword is important for Swift developers working on apps or libraries. It helps write cleaner, safer, and more flexible code.
Progress0 / 4 steps
1
Create the Shape protocol and Circle struct
Create a protocol called Shape with a read-only property area of type Double. Then create a struct called Circle that conforms to Shape and has a property radius of type Double. Implement the area property to return the area of the circle using the formula 3.14159 * radius * radius.
Swift
Hint
Remember, a protocol defines what properties or methods a type must have. The Circle struct must say it conforms to Shape and provide the area property.
2
Create the makeCircle function returning some Shape
Create a function called makeCircle that returns some Shape. Inside the function, return a Circle with a radius of 5.0.
Swift
Hint
The some keyword hides the exact type but promises it conforms to Shape. Just return a Circle with radius 5.0.
3
Call makeCircle and store the result
Call the function makeCircle() and store the result in a constant called shape.
Swift
Hint
Store the result of makeCircle() in a constant named shape.
4
Print the area of the shape
Print the area of the shape stored in shape using print("Area: \(shape.area)").
Swift
Hint
Use print("Area: \(shape.area)") to show the area.
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 }
}