自定义类型作为参数传递给函数 [英] Custom type passed to function as a parameter
问题描述
当我定义自定义类型时,似乎底层类型的类型对我是否可以将其按原样传递给函数或我需要对其进行转换有所不同.
When I define a custom type, it seems that the type of the underlying type makes a difference about whether I can pass it to a function as is or I need to convert it.
问题是:为什么 RuneFunc
和 StringMap
可以工作,而 Integer
不行?
Question is:
Why does RuneFunc
and StringMap
work, but not Integer
?
https://play.golang.org/p/buKNkrg5y-
package main
type RuneFunc func(rune) rune
type Integer int
type StringMap map[string]string
func main() {
//m := make(StringMap)
//mf(m)
var i Integer = 5
nf(i)
//var f func(rune) rune
//ff(f)
}
func mf(i map[string]string) {
}
func ff(i func(rune)rune) {
}
func nf(i int) {
}
在这里,当我使用 Integer
运行这个名为 nf
的函数时,它会抱怨,尽管 int
是基础类型.但是当我调用 mf
或 ff
时,它们运行成功.
Here, when I run this function called nf
with Integer
it complains although int
is the underlying type. But when I call mf
or ff
they run successfully.
推荐答案
Integer
and int
int
和你的新类型 Integer
是两种不同的不同类型.在需要 Integer
的地方,您必须传递 Integer
类型的值.
Integer
and int
int
and your new type Integer
are 2 different, distinct types. Where Integer
is expected, you have to pass a value of type Integer
.
如果您有一个 Integer
值,您可以使用简单的类型转换使其成为 int
类型的值,因为 Integer
是 int
:
If you have an Integer
value, you may use a simple type conversion to make it a value of type int
, because the underlying type of Integer
is int
:
var i Integer = 5
nf(int(i))
同时可能令人困惑和有趣的是,您可以在不进行转换的情况下传递无类型常量:
What may be confusing and interesting at the same time is that you are allowed to pass an untyped constant without conversion:
nf(5)
在 Go Playground 上试试这些.
原因在规范:可分配性:
值 x
可以赋值给变量 类型为 T
(x
可分配给 T
")在以下任何情况下:
A value
x
is assignable to a variable of typeT
("x
is assignable toT
") in any of these cases:
[...]
x
是一个无类型的 constant ,可以用 type 的值表示T
.
x
is an untyped constant representable by a value of typeT
.
5
是一个无类型常量,它可以用 int
类型的值表示,因为无类型常量 5
有一个 默认类型 int
,所以它可以用 Integer类型的值表示代码>(具有相同的默认类型).
5
is an untyped constant which is representable by a value of type int
because the untyped constant 5
has a default type int
, so it is representable by a value of type Integer
(which has the same default type).
如果您检查其他可分配性规则(未包含在上述引用中),则它们都不匹配您尝试为 int<类型的参数传递
Integer
值的情况/code>,所以这是不允许的.
If you check the other assignability rules (not included in above quotation), none of them match the case where you attempt to pass a value of Integer
for the parameter of type int
, so that's not allowed.
参见相关问题:Golang: 创建常量类型并限制类型的值
这种情况与前一种情况(Integer
和 int
)的区别在于 int
是一个 命名类型 和 func(rune) rune
不是.
The difference between this case and the previous one (Integer
and int
) is that int
is a named type and func(rune) rune
is not.
并且有一个可分配性规则允许这样做:
And there's an assignability rule which allows this:
x
's typeV
andT
have identical underlying types and at least one ofV
orT
is not a named type.
所以在这种情况下:
var f RuneFunc
ff(f)
f
是一个命名类型,但是 ff()
的参数类型是未命名的 func(rune) rune
,所以允许赋值.
f
is a named type, but the parameter type of ff()
is func(rune) rune
which is unnamed, so the assignment is allowed.
这篇关于自定义类型作为参数传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!