0
0
Swiftprogramming~10 mins

Opaque types with some keyword in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Opaque types with some keyword
Define function returning some Protocol
Function hides concrete type
Caller gets value typed as Protocol
Use returned value via Protocol interface
Concrete type remains hidden
The function returns a value conforming to a protocol but hides the exact type using 'some'. The caller sees only the protocol interface.
Execution Sample
Swift
protocol Shape {
    func area() -> Double
}

func makeCircle() -> some Shape {
    struct Circle: Shape {
        var radius: Double
        func area() -> Double { 3.14 * radius * radius }
    }
    return Circle(radius: 5)
}
Defines a function returning an opaque type conforming to Shape protocol, hiding the concrete Circle type.
Execution Table
StepActionEvaluationResult
1Call makeCircle()Create Circle(radius: 5)Circle instance created
2Return Circle as some ShapeHide concrete typeReturn value typed as some Shape
3Caller receives valueType is some ShapeCan call area() but not access Circle-specific properties
4Call area() on returned valueCompute 3.14 * 5 * 5Returns 78.5
5EndNo more stepsExecution stops
💡 Function returns opaque type; caller uses protocol interface without knowing concrete type
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
circlenilCircle(radius: 5)Hidden as some Shapesome Shape valuesome Shape value with area()=78.5
Key Moments - 2 Insights
Why can't the caller access Circle's radius property?
Because the function returns 'some Shape', the concrete Circle type is hidden. The caller only knows the value conforms to Shape, so only protocol methods like area() are accessible (see execution_table step 3).
Is the concrete type created each time the function is called?
Yes, each call creates a new Circle instance internally (execution_table step 1), but the caller only sees it as some Shape.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the type of the value returned by makeCircle() at step 2?
Asome Shape
BCircle
CShape protocol
DAny
💡 Hint
See execution_table row 2: the concrete type is hidden and returned as some Shape.
At which step does the caller call the area() method on the returned value?
AStep 1
BStep 2
CStep 4
DStep 3
💡 Hint
Check execution_table row 4: area() is called on the opaque type.
If the function returned Shape instead of some Shape, what would change in the execution?
ACaller could access Circle's radius
BCaller would get a protocol existential instead of opaque type
CFunction would return concrete Circle type
DNo difference
💡 Hint
Opaque types hide concrete type; returning protocol type returns existential (concept explanation).
Concept Snapshot
Use 'some Protocol' to return an opaque type.
The concrete type is hidden from the caller.
Caller sees only the protocol interface.
Allows type safety with abstraction.
Example: func f() -> some Shape { ... }
Full Transcript
This example shows how a Swift function can return an opaque type using the 'some' keyword. The function makeCircle() creates a concrete Circle instance internally but returns it as 'some Shape', hiding the concrete type. The caller receives a value typed as some Shape and can call protocol methods like area(), but cannot access Circle-specific properties like radius. The execution table traces the steps: creating the Circle, returning it as some Shape, the caller receiving it, and calling area(). This helps beginners understand how opaque types provide abstraction while preserving type safety.