Protocol typealias declaration with no assignment

ViewModifier protocol is defined as following:

public protocol ViewModifier {
associatedtype Body : View
func body(content: Self.Content) -> Self.Body
typealias Content
}

how come typealias Content does not have assigment (= Some type), and the compiler does not complain? If I create my own custom protocol is a similar fashion to ViewModifier, the compile complains.

Thanks in advance for your inputs

Regards
Eva

Probably you are looking an interface description, not the actual source code. Where are you reading it?

in the Xcode 11, when you jump to the definition

Yes. That is just a textual interface summary intended for humans.

Doing “Jump to Definition” for NSView causes this to show up:

open class NSView : NSResponder /* ... */ {
    public init(frame frameRect: NSRect)
    public init?(coder decoder: NSCoder)
    unowned(unsafe) open var window: NSWindow? { get }
    unowned(unsafe) open var superview: NSView? { get }
    // ...
}

Notice that none of those have concrete implementations and the whole thing would be invalid as real source.

Whenever the full source of the definition is available (such as if it is elsewhere in the project itself), then “Jump to Definition” does lead straight to the real source definition. It falls back to only showing an API summary like that when the source is unavailable (such as if it comes from Apple’s closed‐source system frameworks).

Hmm. is the source code available to look at somewhere?

I don’t think SwiftUI is open source.

thank you. This is helpful

I had the exact same question upon selecting "Jump to Definition" on ViewModifier as I was curious about the details behind the Content parameter. Bumping into @SDGGiesbrecht's response (that the page is a " interface description" and not actual code that is compiled) earlier would have saved me some time, the but that still doesn't reveal more details about the Content object. For instance, what type is Content really? Why can you invoke methods that are traditionally only applied on View objects (eg. content.scaledToFit())? Why is Content defined as a typealias and not an associatedType?

You can get some insight by invoking the body method on a conforming ViewModifier object and passing in a non-sensical type (eg. Int)

struct MockModifier: ViewModifier {
    func body(content: Content) -> some View {
        return Text("")
    }
}

MockModifier().body(content: 3)
/// error: cannot convert value of type 'Int' to expected argument type 'MockModifier.Content' (aka '_ViewModifier_Content<MockModifier>')

From the above, I image the actual source code would look something like this-

public protocol ViewModifier {
    associatedtype Body : View
    typealias Content = _ViewModifier_Content<Self>

    @ViewBuilder func body(content: Self.Content) -> Self.Body
}

The _ should indicate that it's a private type and something that should not really concern the client. The typealias provides a more expressive syntax when a client specifies the body method for the conforming ViewModifier. For instance, func body(content: Content) -> some View places a significantly lower mental load when reading the method compared to func body(content: _ViewModifier_Content<MockModifier>) -> some View).