Additional notes on "Phantom Architecture" 4: Application scenarios and limitations of the Protocol

Source: Internet
Author: User

Explain again the meaning of the Protocol: Define the minimum granularity of a functional module, because Swift is a single inheritance, and whether a value type or a reference type can follow multiple protocols, the protocol is a functional module that is smaller than the granularity of the parent class. The functional definition of the protocol must be specific and strict, someobject:protocol where ... The match condition in the WHERE clause can only be for someobject types or other protocols that are adhered to, and Associatedtype in protocol, which means that the protocol cannot be respected based on the members of the Someobject. Examples are as follows:
Many of the underlying types in the SWIFT standard library adhere to the Equatable protocol, which requires the user to define a = = method, which indicates that although each type is judged to be of equal standard, in any case two of the same type that complies with the Equatable protocol can be sentenced, This is the work that the agreement has done for us. Considering our commonly used arrays, the = = operation can also be used in array, but the array does not adhere to the Equatable protocol, because the compiler makes a restriction when invoking the = = method of the array instance, only when the elements stored in the array adhere to the Equatable protocol , array can be sentenced, so array is not under any conditions can be sentenced, etc., does not conform to the function of the Equatable protocol description. In this case we need to define a method directly:

func ==<T: Equatable>(lhs: [T], rhs: [T]) -> Bool {      //对比las和rhs中的元素是否相等}

The advantage of the method is that you can refer to any generic type as a placeholder to describe the function, such as t here, so that you can spec two parameters must be of the same type of array, and the elements adhere to equatable. When an element in an array does not conform to equatable, the compile-time call = = will receive a compiler error.
And when using the protocol, Someobject:protocol where ... Where you can only constrain the Associatedtype declared in the protocol, you cannot introduce an additional placeholder such as T to describe the constraint.

Additional notes on "Phantom Architecture" 4: Application scenarios and limitations of the Protocol

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.