Swift generics and generic functions

Source: Internet
Author: User

Generics (generic) allow us to define a number of mutable parts in the program code that are specified at run time. Generics can be used to maximize code reuse, protect types of security, and improve performance. In the Swift collection class, generics are already in use.
Reflection on the problem of one
How do you define a function to determine whether the two parameters are equal?
If the argument is of type int, the function is defined as follows:
Func isequalsint (A:int, b:int), Bool {
return (A = = b)
}
This function argument list is a two int type, which can only compare two int type parameters for equality. If we want to compare the equality of two double types, you can modify the functions defined above as follows:
Func isequalsdouble (a:double, b:double), Bool {
return (A = = b)
}
This function argument list is a two double type, which can only compare two double type parameters for equality. If we want to compare two string types for equality, you can modify the functions defined above as follows:
Func isequalsstring (a:string, b:string), Bool {
return (A = = b)
}
Above, we compare 3 different types, and define similar 3 functions. So can we define 1 functions to compare 3 different types? If the int, double, and string after the 3 function names of Isequalsint, isequalsdouble, and isequalsstring are mutable, then these mutable parts are associated with the parameter type.
Second, generic function
We can modify the above function, the contents are as follows:
Func isequals<t> (A:t, b:t), Bool {①
return (A = = b)
}
Add <t&gt after the function name Isequals, and the type of the parameter is also declared as t,t, called a placeholder, which is passed to the actual type each time it is called to determine what type T represents. If you have more than one type, you can use other uppercase letters, generally we are accustomed to using the U-letter, but you can also use other letters. Multiple placeholders are delimited with a comma, "," as in the following example:
Func isequals<t, u> (A:t, b:u), Bool {...}
Placeholders are not just alternative to parameter types, they can also override return value types. The sample code is as follows:
Func isequals<t> (A:t, b:t), T {...}
In fact, the functions on line ① above will have errors at compile time, because not all types are "comparable" and they must conform to the comparable protocol implementation type. The comparable protocol represents comparable, and in Swift, the basic data types and strings are subject to the comparable protocol.
Modify the code as follows:

Func isequals<t:comparable> (A:t, b:t), Bool {②    return (A = = b)}


We need to add a colon and a protocol type after the T-placeholder, which is called a generic constraint, and it can replace the type of T. In this example, the type of t must conform to the specific class of the comparable protocol.
We can test the function defined by the ② line code with the following code:
Let N1 = 200let N2 = 100println (Isequals (N1, N2)) Let S1 = "ABC1" Let S2 = "ABC1" println (isequals (S1, S2))


The results are as follows: two int parameters and string parameters are passed separately for comparison.
False
True
The results of the operation are not explained. Generics are used in many computer languages, and the basic meaning is similar, but the small difference is still there.



For more information, please visit the first Swift book "Swift Development Guide" book Exchange discussion website: http://www.51work6.com/swift.php Welcome to join Swift Technical discussion group: 362298485

Welcome to Luxgen iOS Classroom public Platform


Swift generics and generic functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.