Here’s a generic version of the same code:

  1. struct Stack<Element> {
  2. var items = [Element]()
  3. mutating func push(_ item: Element) {
  4. items.append(item)
  5. }
  6. mutating func pop() -> Element {
  7. return items.removeLast()
  8. }
  9. }

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

Element defines a placeholder name for a type to be provided later. This future type can be referred to as Element anywhere within the structure’s definition. In this case, Element is used as a placeholder in three places:

  • To create a property called items, which is initialized with an empty array of values of type Element
  • To specify that the push(_:) method has a single parameter called item, which must be of type Element
  • To specify that the value returned by the pop() method will be a value of type Element

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

Generic programming is a style of computer programming in which algorithms are written in terms of types to-be-specified-later that are then instantiated when needed for specific types provided as parameters.

template<typename T>
class List
{
/* class contents */
}; List<Animal> list_of_animals;
List<Car> list_of_cars;

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

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

泛型术语:占位类型placeholder的更多相关文章

  1. tensorflow中常量(constant)、变量(Variable)、占位符(placeholder)和张量类型转换reshape()

    常量 constant tf.constant()函数定义: def constant(value, dtype=None, shape=None, name="Const", v ...

  2. Java中如何获取一个类中泛型的实际类型

    本文链接:https://blog.csdn.net/kuuumo/article/details/83021158   _______________________________________ ...

  3. [Sass]占位符 %placeholder

    [Sass]占位符 %placeholder Sass 中的占位符 %placeholder 功能是一个很强大,很实用的一个功能,这也是我非常喜欢的功能.他可以取代以前 CSS 中的基类造成的代码冗余 ...

  4. 泛型T的类型获取

    T.getClass()或者T.class都是非法的,因为T是泛型变量. 由于一个类的类型是什么是在编译期处理的,故不能在运行时直接在Base里得到T的实际类型. /** * 可以在service层直 ...

  5. 使用C#反射中的MakeGenericType函数,来为泛型方法和泛型类指定(泛型的)类型

    C#反射中的MakeGenericType函数可以用来指定泛型方法和泛型类的具体类型,方法如下面代码所示这里就不多讲了,详情看下面代码一切就清楚了: using System; using Syste ...

  6. Java泛型-内部原理: 类型擦除以及类型擦除带来的问题

    一:Java泛型的实现方法:类型擦除 大家都知道,Java的泛型是伪泛型,这是因为Java在编译期间,所有的泛型信息都会被擦掉,正确理解泛型概念的首要前提是理解类型擦除.Java的泛型基本上都是在编译 ...

  7. 占位符(placeholder text)

    占位符(placeholder text)是用户在input(输入)框输入任何东西之前放置在input(输入)框中的预定义文本. 你可以用如下方式创建占位符: <input type=" ...

  8. C# 泛型多种参数类型与多重约束 示例

    C# 泛型多种参数类型与多重约束 示例 interface IMyInterface { } class Dictionary<TKey, TVal> where TKey : IComp ...

  9. Gson通过借助TypeToken获取泛型参数的类型的方法

    最近在使用Google的Gson包进行Json和Java对象之间的转化,对于包含泛型的类的序列化和反序列化Gson也提供了很好的支持,感觉有点意思,就花时间研究了一下. 由于Java泛型的实现机制,使 ...

随机推荐

  1. springmvc的异常统一处理

    在项目实际开发中,异常的统一处理是一个常态.假如不使用异常统一处理,我们往往需要在service层中捕获异常,并且根据不同的异常在result中的设置不同的code并给予相应的提示.这样可能会导致不同 ...

  2. Adding Security to an Existing Website

    The procedure earlier in this article relies on using the Starter Site template as the basis for web ...

  3. codeforces 688A A. Opponents(水题)

    题目链接: A. Opponents time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  4. 杂项:WebRTC

    ylbtech-杂项:WebRTC WebRTC,名称源自网页实时通信(Web Real-Time Communication)的缩写,是一个支持网页浏览器进行实时语音对话或视频对话的技术,是谷歌20 ...

  5. Luogu P1638 逛画展 【二分答案】

    题目描述 博览馆正在展出由世上最佳的 M 位画家所画的图画. wangjy想到博览馆去看这几位大师的作品. 可是,那里的博览馆有一个很奇怪的规定,就是在购买门票时必须说明两个数字, a和b,代表他要看 ...

  6. 图的遍历---------开始开始-------o(∩_∩)o 哈哈

    图的遍历 深度优先搜索(Depth First Search , DFS) --深度优先搜索--我的理解就是分身术的另一种实现方法---用分身术将所有能看到的路都走一遍----这就是深度搜索--- 下 ...

  7. 例题3-4 master-mind hints

    下面先附上我的水货代码,,,,一会附上,,,刘大婶给的代码///////3ms #include<stdio.h> #include<string.h> int main() ...

  8. Hdu 5336 XYZ and Drops (bfs 模拟)

    题目链接: Hdu 5336 XYZ and Drops 题目描述: 有一个n*m的格子矩阵,在一些小格子里面可能会有一些水珠,每个小水珠都有一个size.现在呢,游戏开始咯,在一个指定的空的小格子里 ...

  9. JDK6中System.getProperties返回键值说明

    JDK6中java.lang.System.getProperties()方法返回键值说明. 键 相关值的描述 java.version Java 运行时环境版本 java.vendor Java 运 ...

  10. Resources.getSystem() 与 getResources()区别

    参考: http://stackoverflow.com/questions/8633539/resources-getsystem-vs-getresources 相同: 都是取得 Resource ...