Something odd with the "source" of Morphology in Foundation

I'm looking at Morphology in Foundation and see this very odd source: it's as if copy-paste straight from protocol RawRepresentable, the doc comments for the init?() and var rawValue are the same from the protocol:

    @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
    public enum GrammaticalGender : Int, Hashable {

        case feminine

        case masculine

        case neuter

        /// Creates a new instance with the specified raw value.
        ///
        /// If there is no value of the type that corresponds with the specified raw
        /// value, this initializer returns `nil`. For example:
        ///
        ///     enum PaperSize: String {
        ///         case A4, A5, Letter, Legal
        ///     }
        ///
        ///     print(PaperSize(rawValue: "Legal"))
        ///     // Prints "Optional("PaperSize.Legal")"
        ///
        ///     print(PaperSize(rawValue: "Tabloid"))
        ///     // Prints "nil"
        ///
        /// - Parameter rawValue: The raw value to use for the new instance.
        public init?(rawValue: Int)

        /// The raw type that can be used to represent all values of the conforming
        /// type.
        ///
        /// Every distinct value of the conforming type has a corresponding unique
        /// value of the `RawValue` type, but there may be values of the `RawValue`
        /// type that don't have a corresponding value of the conforming type.
        public typealias RawValue = Int

        /// The corresponding value of the raw type.
        ///
        /// A new instance initialized with `rawValue` will be equivalent to this
        /// instance. For example:
        ///
        ///     enum PaperSize: String {
        ///         case A4, A5, Letter, Legal
        ///     }
        ///
        ///     let selectedSize = PaperSize.Letter
        ///     print(selectedSize.rawValue)
        ///     // Prints "Letter"
        ///
        ///     print(selectedSize == PaperSize(rawValue: selectedSize.rawValue)!)
        ///     // Prints "true"
        public var rawValue: Int { get }
    }

there is no need to write the implementation of RawValue, declaring the enum conform to Int is enough. Is this not the real source but some kind of machine generated "fake representation"?

Yes, it's a known problem (or limitation, perhaps) in so-called "generated headers" in Swift. For a given type, it includes all of the declarations in the type, even if inherited from a protocol to which the type conforms. When the declarations are inherited, the documentation comments from the original source (the protocol) are replicated, too.

This sort of makes sense. If you "Jump to Definition" for the type, you want to see everything that the type can do. It would be awful if you had to jump again to find declarations inherited from protocols (especially if you didn't know which protocol you needed to look at).

OTOH, it can be very confusing because the replicated comments often don't apply very well to the type.

2 Likes
Terms of Service

Privacy Policy

Cookie Policy