SwiftMidMCQ
What is `some` keyword (opaque types)?
Test your knowledge:
Explanation & Code
Answer:
some declares an opaque return type — the function returns a specific concrete type that conforms to a protocol, but the caller doesn't need to know which type it is. Used heavily in SwiftUI's body: some View.
Code Example:
// Without some — must specify exact type (often impossible with complex views)
func makeView() -> VStack<TupleView<(Text, Text)>> { ... } // ugly
// With some — hides the concrete type
func makeView() -> some View {
VStack {
Text("Hello")
Text("World")
}
}
// Also useful for protocols with associated types
protocol Shape {
func area() -> Double
}
func makeShape() -> some Shape {
Circle(radius: 5)
}
Key difference from any: some is a specific type (compiler-optimized), any is a type-erased existential (runtime overhead).
Rate your understanding: