Search code examples
swifttype-annotation

Swift's definition of Type Annotation


In Swift Programming Language guide, it has this definition for Type Annotation: "A type annotation explicitly specifies the type of a variable or expression."

We all know how to specify the type of a variable, but how exactly do you specify the type of an expression?


Solution

  • The Apple documentation says: "in

    let someTuple: (Double, Double) = (2.14159, 2.71828)
    

    the expression someTuple is specified to have the tuple type..." (emphasis mine). Thus a let or var binding is an example using a type annotation on an expression.

    A type annotation for a variable is illustrated as specifying the type for a function parameter:

    func someFunction(a:Int) { /* ... */ }