Generics are introduced in Apple's new programming language "Swift". Generics are one of the most powerful features of Swift, and much of the Swift standard library is built with generic code.
2. Generics?
Generics are one of the most powerful feature of
swift.
Generic code enables you to write flexible,
reusable functions and types that can work with any
type, subject to requirements that you define.
For example, Swifts Array and Dictionary types are
both generic collections. Means they can hold array
of Int values or String values or any type.
3. Type Parameters
The placeholder type T is an example of a type
parameter.
Type parameters specify and name a placeholder
type, and are written immediately after the functions
name, between a pair of matching angle brackets
(such as <T>).
You can provide more than one type parameter by
writing multiple type parameter names within the
angle brackets, separated by commas.
4. Why Generics will be showstopper?
The generic version of the function uses a
placeholder type name (called T, in this case) instead
of an actualtype name (such as Int, String, or Double).
The generic functions name (swapTwoValues) is
followed by the placeholder type name (T) inside
angle brackets (<T>). The brackets tell Swift that T is a
placeholder type name within the swapTwoValues
function definition. Because T is a placeholder, Swift
does not look for an actual type called T.
5. Type Parameters
The generic version of the function uses a
placeholder type name (called T, in this case) instead
of an actualtype name (such as Int, String, or Double).
The generic functions name (swapTwoValues) is
followed by the placeholder type name (T) inside
angle brackets (<T>). The brackets tell Swift that T is a
placeholder type name within the swapTwoValues
function definition. Because T is a placeholder, Swift
does not look for an actual type called T.
You can provide more than one type parameter by
writing multiple type parameter names within the
angle brackets, separated by commas.
6. Naming of Type Parameters
It is traditional to use the single-character name T for
the type parameter. However, you can use any valid
identifier as the type parameter name.
If you are defining more complex generic functions,
or generic types with multiple parameters, it is useful to
provide more descriptive type parameter names. For
example, Swifts Dictionary type has two type
parametersone for its keys and one for its values.
Always give type parameters UpperCamelCase
names (such as T and Key) to indicate that they are a
placeholder for a type, not a value.
7. Generic Types
In addition to generic functions, Swift enables you to
define your own generic types. These are custom
classes, structures, and enumerations that can work
with any type, in a similar way to Array and Dictionary.
Below is example of how to write generic
collection type called Stack.
struct Stack<T> {
var items = [T]()
mutating func push(item: T) { //mutating, because
they need to modify (or mutate) the structures items
array
items.append(item)
}
mutating func pop() -> T { return items.removeLast()
}
}
8. Extending a Generic Type
When you extend a generic type, the type
parameter list from the original type definition is
available within the body of the extension, and the
original type parameter names are used to refer to
the type parameters from the original definition.
For example:
extension Stack { // extends the generic Stack type to
add a read-only computed property called topItem
var topItem: T? {
return items.isEmpty ? nil : items[items.count - 1]
}
}
9. Type Constraints
It is sometimes useful to enforce certain type
constraints on the types that can be used with generic
functions and generic types.
Type constraints specify that a type parameter must
inherit from a specific class, or conform to a particular
protocol or protocol composition.
The basic syntax for type constraints on a generic
function is shown below (although the syntax is the
same for generic types):
func someFunction<T: SomeClass, U:
SomeProtocol>(someT: T, someU: U) {
// function body goes here
}
10. Associated Types
An associated type gives a placeholder name (or
alias) to a type that is used as part of the protocol.
The actual type to use for that associated type is not
specified until the protocol is adopted.
Associated types are specified with the typealias
keyword.
Where Clauses: A where clause enables you to
require that an associated type conforms to a certain
protocol, and/or that certain type parameters and
associated types be the same.
You write a where clause by placing the where
keyword immediately after the list of type parameters,
followed by one or more constraints for associated
types, and/or one or more equality relationships
between types and associated types.