2021swift parameter and generic parameter reference!


This section covers generic types, generic functions, and parameters of generic constructors, including formal and arguments. When declaring a generic type, function, or constructor, you must specify the corresponding type parameters. A type parameter is equivalent to a placeholder. When a generic type is instantiated, a generic function or a generic constructor is called, it is replaced with a specific type argument.
2021swift parameter and generic parameter reference!

For an overview of the generics of swift language, see generics (Part II, Chapter 22).

Generic parameter statement

Generic parameter statements specify the type parameters of a generic type or function, and the associated constraints and requirements for these parameters. Generic parameter statements are enclosed by angle brackets (< >), and have the following two forms:

1.  <generic parameter list> <generic parameter list where requirements > 

1.  <generic parameter list> <generic parameter list where requirements > 

The generic parameters in the generic parameter list are separated by commas, and each takes the following form:

1.  type parameter : constrain 

Generic parameters consist of two parts: type parameters and optional constraints after them. Type parameters are just the names of placeholder types (such as t, u, V, keytype, ValueType, etc.). You can use it in generic types, the rest of functions or constructor declarations, and the signatures of functions or constructors.
Constraints are used to indicate that the type parameter inherits from a class or complies with a protocol or part of a protocol. For example, in the following generic, the generic parameter t: comparable means that any type argument used to replace the type parameter t must meet the comparable protocol.

1.  func simpleMin<T: COmparable>(x: T, y: T) -> T {
2.  if x < y {
3.  return y
4.  }
5.  return x
6.  } 

For example, both int and double meet the comparable protocol, and this function accepts any type. In contrast to generic types, you do not need to specify generic argument statements when calling generic functions or constructors. Type arguments are inferred from arguments passed to a function or constructor.

1.  simpleMin(17, 42)
2.  // T is inferred to be Int
3.  simpleMin(3.14159, 2.71828)
4.  // T is inferred to be Double 

Data collection office

Where statement

To specify additional requirements for type parameters and their associated types, you can add a where statement after the generic parameter list. The where statement consists of the keyword where and its subsequent comma separated requirements.

The requirements in the where statement are used to indicate that the type parameter inherits from a class or complies with a protocol or part of a protocol. Although the where statement helps to express simple constraints on type parameters (for example, t: comparable is equivalent to t where, t: comparable, etc.), it can still be used to provide more complex constraints on type parameters and their associated constraints. For example, < T where t: C, t: P > indicates that the generic type T inherits from class C and complies with protocol P.

As mentioned above, the association type of the constraint type parameter can be forced to comply with a protocol< T: Generator where t.element: equable > means that t complies with the generator protocol, and t’s association type t.element complies with the eauatable protocol (t has an association type because the generator declares an element, while t complies with the Generator Protocol).

You can also use the operator = = to specify the equivalent requirements of two types. For example, there is such a constraint: T and u comply with the generator protocol, and their association types are required to be the same. It can be expressed as follows: < T: generator, u: generator where t.element = = u.element >.

Of course, type arguments that replace type parameters must meet the constraints and requirements required by all type parameters.

Generic functions or constructors can be overloaded, but type parameters in generic parameter statements must have different constraints or requirements, or both. When an overloaded generic function or constructor is called, the compiler uses these constraints to determine which overloaded function or constructor to call.

A generic class can generate a subclass, but the subclass must also be a generic class.

Syntax of generic parameter clauses.
Parameter clause → < generic parameter list ­ Requirement – clause >.
Generic parameter list → generic parameter list, generic parameter list.
Generic parameter → type name.
Generic parameter → type name: type identifier.
Generic parameter protocol type name: → composition type.
Requirements clause → where? Requirements-List?
Consistency – Requirements → type identifier: type identifier.
Consistency – protocol type required – identifier: → – combination – type.
Same type requirements → type identifier ­== Type identifier

Generic argument statement

Generic argument statementspecifies the type argument of a generic type. The generic argument statement is enclosed by angle brackets (< >), as follows:

1.  < generic argument list > 

Type arguments in the generic argument list are separated by commas. The type argument is the name of the actual concrete type, which is used to replace the corresponding type parameter in the generic parameter statement of the generic type. This results in a specialized version of the generic type. For example, the generic dictionary type of swift standard library is defined as follows:

1.  struct Dictionary<KeyTypel: Hashable, ValueType>: Collection, DictionaryLiteralConvertibl
e { /* .. */ } 

A specialized version of the generic dictionary type. Dictionary < string, int > is generated by replacing the generic types keytype: hashable and ValueType with specific string and int types. Each type argument must satisfy all the constraints of the generic parameter it replaces, including the additional requirements specified by any where statement. In the above example, the type parameter keytype must meet the hashable protocol, so the string must also meet the hashable protocol.

Type parameters can be replaced by type arguments that are themselves specialized versions of generic types (assuming that appropriate constraints and requirements have been met). For example, in order to generate an array whose element type is an integer array, you can replace the type parameter t of the generic type array with the specialized version of array array.

1.  let arrayOfArrays: Array<Array<Int>> = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 

As described in the generic parameter statement, a generic argument statement cannot be used to specify the type arguments of a generic function or constructor.

The grammar of generic argument clauses.
Generic self variable quantum Sentence List < – Generic – parameter – List – > →.
Generic parameter list → generic parameter, generic parameter, generic parameter list.
Generic parameter → type

Data collection office

Due to the limited space of the article, you can only point to introduce some current work achievements and thoughts. Each Swift has some new directions to explore. If you are interested in understanding the underlying principle of IOS, architecture design, system construction and how to interview, you can also pay attention to me to obtain the latest information and interview related information in time. If you have any comments and suggestions, please leave me a message!

You are welcome to point out the bad places. I hope you can leave more messages and discuss them. Let’s make progress together!

Friends who like IOS can pay attention to me and learn and communicate together!!!