Search code examples
swiftclassdocumentationinstancenotation

Swift documentation: instance/type property/method notation


To document Ruby, I'd write, eg, Time::now or Time#day. How do I document Swift?

That is, when documenting Swift, what is the notation for a type and its 1) type property or method or 2) instance property or method?

For example, in Ruby documentation, the symbol :: (two colons) denotes a class property or method, and the symbol # (number sign, hash, hashtag, or pound sign) denotes an instance property or method. So, Time::now means that now is a class property or method of Time, and Time#day means that day is an instance property or method of Time.

Does Swift documentation have such a notation syntax?

I know Swift documentation's function notation—for example, that the Swift append(_ newElement: Element) method for Array is documented as append(_:)—because I see many examples of this notation in Apple's documentation. But, how do I write Array#append(_:) for Swift?


Solution

  • Unfortunately Swift does not have an official or widely-accepted notation to distinguish type properties/methods and instance properties/methods with type name prefixed form.

    (So, usual Swift programmers (even expert) cannot understand what you are asking.)

    Type name prefixed form is actually used in the Swift book, but not so often.

    As far as I checked:

    • In some parts, type property is referred to in a form like UInt32.max, but as you see this is just using actual notation valid as Swift expression.

    • In some other parts, type method is referred to as a form like LevelTracker.unlock(_:), but this also is a valid expression in Swift and I'm not sure Apple is using this as a documentation notation for type method. I cannot find an example in the Swift book with a short glance, but initializers are often referred to in a form like String.init(data:encoding:) and this is also a valid expression in Swift.

    • For other cases, instance methods or properties are referred to as instanceVar.methodName(_:) or instanceVar.propertyName, of course instanceVar appears in a nearby code snippet and is not a type name, this actually is not what you are looking for.

    And as you already know, in Apple's official references, methods or properties are shown with heading Instance method, Type method , Instance Property or Type Property. Or prefixed with class/static var/let, class/static func, var/let or func.

    I cannot find an example with a very short survey, but some articles (including Apple's) may be referring to an instance method also in a form TypeName.methodName(_:) (or instance property as well.) Seems Swift community thinks distinguishing type members and instance members is not important.

    I could not take much time, but seems it is obvious that

    Swift does not have an official or widely-accepted notation to distinguish type properties/methods and instance properties/methods with type name prefixed form.

    Maybe you need to write something like instance method Array.append(_:) to represent Array#append(_:).

    (To note, Array.append(_:) is also a valid expression in Swift.)