Here's a generic version of the same code:
- struct stack<element> {
- var items = [element] ()
- mutating func push (_ Span class= "NV" >item: element) {
- items. append (item)
- }
- mutating func pop () element {
- return items.
- }
- }
Note how the generic version Stack
of was essentially the same as the nongeneric version, but with a type parameter called Element
instead of an actual type of Int
. This type parameter are written within a pair of angle brackets ( <Element>
) immediately after the structure ' s name.
Element
Defines 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