美文网首页
Type Parameters

Type Parameters

作者: 幸运的小强本人 | 来源:发表于2016-03-07 07:17 被阅读16次

    In the swapTwoValues example above, the placeholder type T is an example of a type parameter. Type parameters specify and name a placeholder type, and are written immediately after the function's name, between a pair of matching angle brackets (such as <T>).

    Once you specify a type parameter, you can use it to define the type of a function's parameters (such as the a and b parameters of the swapTwoVlaues function), or as the function's return type, or as a type annotation within the body of the function. In each case, the placeholder type represented by the type parameter is replaced with an actual type whenever the function is called. (In the swapTwoValues example above, T was replaced with Int the first time the function was called, and was replaced with String the second time it was called.)

    You can provide more than one type parameter by writing multiple type parameter names within the angle brackets, separated by commas.

    相关文章

      网友评论

          本文标题:Type Parameters

          本文链接:https://www.haomeiwen.com/subject/xcbakttx.html