Generic term: Placeholder type placeholder

Source: Internet
Author: User

Here's a generic version of the same code:

    1. struct stack<element> {
    2. var items = [element] ()
    3. mutating func push (_ Span class= "NV" >item: element) {
    4. items. append (item)
    5. }
    6. mutating func pop () element {
    7. return items.
    8. }
    9. }

Note how the generic version Stack of was essentially the same as the nongeneric version, but with a type parameter called Elementinstead of an actual type of Int . This type parameter are written within a pair of angle brackets ( <Element> ) immediately after the structure ' s name.

ElementDefines a placeholder name for a type to is provided later. This future type can is referred to as Element anywhere within the structure ' s definition. In the used as Element a placeholder in three places:

    • To create a property called items , which was initialized with an empty array of values of typeElement
    • push(_:)to specify that the method have a single parameter called item , which must is of typeElement
    • To specify this value returned by the method would be pop() a value of typeElement

Https://docs.swift.org/swift-book/LanguageGuide/Generics.html

Generic Programming is a style of computer programming in which algorithms be written in terms of types To-b E-specified-later that is then instantiated if needed for specific types provided as parameters.

Template<T>List/* Class Contents */};  List<Animallist_of_animals;  List<Carlist_of_cars;             

Above, is T a placeholder for whatever type was specified when the list is created.

Https://en.wikipedia.org/wiki/Generic_programming

Generic term: Placeholder type placeholder

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.