Types Flashcards
What two kinds of types are there in Swift?
In Swift, there are two kinds of types: named types and compound types.
A named type is a type that can be given a particular name when it is defined.
A compound type is a type without a name, defined in the Swift language itself.
What varieties of named types are there?
Named types include classes, structures, enumerations, and protocols.
In addition to user-defined named types, the Swift standard library defines many commonly used named types, including those that represent arrays, dictionaries, and optional values.
Data types that are normally considered basic or primitive in other languages—such as types that represent numbers, characters, and strings—are actually named types, defined and implemented in the Swift standard library using structures.
What varieties of compound types are there?
There are two compound types: function types and tuple types.
A compound type may contain named types and other compound types.
For instance, the tuple type (Int, (Int, Int)) contains two elements: The first is the named type Int, and the second is another compound type (Int, Int).
In the Swift grammar, type → …
type → array-type | function-type | type-identifier | tuple-type | optional-type | implicitly-unwrapped-optional-type | protocol-composition-type | metatype-type
What is a type annotation?
A type annotation explicitly specifies the type of a variable or expression. Type annotations begin with a colon (:) and end with a type.
Type annotations can contain an optional list of type attributes before the type, such as @auto_closure or @noreturn.
What is a type identifier
A type identifier refers to either a named type or a type alias of a named or compound type.
Most of the time, a type identifier directly refers to a named type with the same name as the identifier. For example, Int is a type identifier that directly refers to the named type Int, and the type identifier Dictionary directly refers to the named type Dictionary.
What are the two cases in which a type identifier does not refer to a type with the same name?
In the first case, a type identifier refers to a type alias of a named or compound type.
In the second case, a type identifier uses dot (.) syntax to refer to named types declared in other modules or nested within other types.
What is a tuple type?
A tuple type is a comma-separated list of zero or more types, enclosed in parentheses.
You can use a tuple type as the return type of a function to enable the function to return a single tuple containing multiple values. You can also name the elements of a tuple type and use those names to refer to the values of the individual elements. An element name consists of an identifier followed immediately by a colon (:).
What is the Void type?
Void is a typealias for the the empty tuple type, ().
What is a function type?
A function type represents the type of a function, method, or closure and consists of a parameter and return type separated by an arrow (->):
parameter type -> return type
Because the parameter type and the return type can be a tuple type, function types support functions and methods that take multiple paramaters and return multiple values.
When can you apply an @auto_closure attribute to a function type?
You can apply the @auto_closure attribute to a function type that has a parameter type of () and that returns the type of an expression.
What does an autoclosure function do?
An autoclosure function captures an implicit closure over the specified expression, instead of the expression itself. The following example uses the auto_closure attribute in defining a very simple assert function:
func simpleAssert(condition: @auto_closure () -> Bool, message: String) { if !condition() { println(message) } }
let testNumber = 5 simpleAssert(testNumber % 2 == 0, "testNumber isn't an even number.") // prints "testNumber isn't an even number."
What are the rules for variadic parameter types?
A function type can have a variadic parameter as the last parameter in its parameter type.
Syntactically, a variadic parameter consists of a base type name followed immediately by three dots (…), as in Int…
A variadic parameter is treated as an array that contains elements of the base type name. For instance, the variadic parameter Int… is treated as Int[].
How do you specify an in-out parameter in a function type?
To specify an in-out parameter, prefix the parameter type with the inout keyword.
You cannot mark a variadic parameter or a return type with the inout keyword.
What is a curried function type?
The type of a curried function is equivalent to a nested function type. For example, the type of the curried function addTwoNumbers()() below is Int -> Int -> Int:
func addTwoNumbers(a: Int)(b: Int) -> Int { return a + b } addTwoNumbers(4)(5) // Returns 9
The function types of a curried function are grouped from right to left. For instance, the function type Int -> Int -> Int is understood as Int -> (Int -> Int)