内置函数是唯一可以接受类型参数的函数吗?

huangapple go评论64阅读模式
英文:

Are the builtins the only functions that can accept a type parameter?

问题

make函数接受一个类型参数。

例如:make([]int, 3)

这只是一种特殊的语法用于内置函数,还是我们可以创建接受类型参数的函数(当然不包括反射)?

英文:

The make function takes a type parameter.

i.e. make([]int, 3)

Is this just a special syntax for builtins or can we create functions that take types (without reflection of course).

答案1

得分: 1

不,你不能。

它们是特殊情况。builtin 包只是用于文档目的,实际上没有办法接受像那样的类型参数。你可能已经注意到 Go 不支持泛型。

英文:

No - you cannot.

They are special cases. The builtin package is there purely for documentation purposes .. there is no actual way for you to accept a type parameter like that. You may have already seen that Go does not support generics.

答案2

得分: 0

这似乎是一种仅限内部使用的语法。

即使在文档中,他们也指出在func make(Type, size IntegerType) Type中,Type参数只是一个代表任何Go类型的替代符号,仅在文档中明确说明。

来源:https://golang.org/src/builtin/builtin.go

英文:

It looks like a built-in only syntax.

Even in the documented source, they note that the in func make(Type, size IntegerType) Type the
Type parameter is just a stand-in for any Go-type and is only explicitly stated for documentation purposes only.

Source: https://golang.org/src/builtin/builtin.go

huangapple
  • 本文由 发表于 2015年3月22日 10:02:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/29190165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定