Opaque return types
ç´é Opaque types çä¸äºè³è¨ã
struct ContentView: View {
var body: some View {
Text("hello world")
}
}
public protocol View {
/// The type of view representing the body of this view.
associatedtype Body : View
/// The content and behavior of the view.
@ViewBuilder var body: Self.Body { get }
}
æ¤ ContentView å®ç¾© body ç Read-Only computed propertiesï¼getter åå³ some Viewï¼æå符å View protocol çåå¥ï¼ç¨±çº Opaque typeã
å®ç¶²å®ç¾©ï¼
A function or method with an opaque return type hides its return valueâs type information. Instead of providing a concrete type as the functionâs return type, the return value is described in terms of the protocols it supports. Hiding type information is useful at boundaries between a module and code that calls into the module, because the underlying type of the return value can remain private. Unlike returning a value whose type is a protocol type, opaque types preserve type identityâthe compiler has access to the type information, but clients of the module donât.
ç±ä»¥ä¸èªªæå¯ç¥ï¼Opaque return type å¯ä»¥é±èåå¥è³è¨ã
æåå¯ä»¥èè以䏿¹æ³ä¾å¾ç¥ç實ç body åå¥è³è¨ãå¨éåä¾åä¸ï¼ç實åå³çå奿¯ VStack<Button<Text>>ã
struct ContentView: View {
var body: some View {
VStack {
Button("print my type") {
print(type(of: self.body))
// VStack<Button<Text>>
}
}
}
}














