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. ajax跨域问题解决(spring boot)

    之前用的服务器响应头部添加Access-Control-Allow-Origin: *来解决的 public static void setResp(HttpServletResponse resp) ...

  2. bzoj2683(要改一点代码)&&bzoj1176: [Balkan2007]Mokia

    仍然是一道cdq模版.. 那么对于一个询问,容斥一下分成四个,变成问(1,1)~(x,y),那么对于x,y,修改只有x'<=x&&y'<=y,才对询问有影响,那么加上读入顺 ...

  3. wirte function in powershell

    https://github.com/dahlbyk/posh-git/blob/master/src/Utils.ps1#L102 https://docs.microsoft.com/en-us/ ...

  4. JFreeChart教程(二)(转)

    JFreeChart教程(二) 分类: java Component2007-05-31 17:01 8408人阅读 评论(11) 收藏 举报 jfreechartstringplotclassdat ...

  5. python compileall 打包发布

    Python工程的编译.合并.打包.发布   from:https://my.oschina.net/bfbd/blog/864310 确保要发布的包(demo)的根目录中有__main__.py文件 ...

  6. 更改Identity用户信息

    1.用当前上下文对象初始化为一个一个UserStote 对象,然后使用 userstore对象 初始化一个UserManagement对象.找到这个对象,然后更新它的部分属性.然后调用 manager ...

  7. window切换Java版本原因

    查找Path环境变量的变量指向的目录,有一个Oracle目录存放着几个 java,javac等可执行文件,删除这个路径或文件就可以执行你指定的JavaHome目录拉 详情参考: https://blo ...

  8. appium封装显示等待Wait类和ExpectedCondition接口

    此文已由作者夏鹏授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 使用WebDriver做Web自动化的时候,org.openqa.selenium.support.ui中提供 ...

  9. E20170516-gg

    accelerator  n. 加速器;油门 oscillator  n. 振荡器; 振子; oscillate  vt. 使振荡,使振动  vi. 持续周期性地摆动; frame  n. 框架; 边 ...

  10. Codeforces 451E Devu and Flowers【容斥原理+卢卡斯定理】

    题意:每个箱子里有\( f[i] \)种颜色相同的花,现在要取出\( s \)朵花,问一共有多少种颜色组合 首先枚举\( 2^n \)种不满足条件的情况,对于一个不被满足的盒子,我们至少拿出\( f[ ...