英文:
Custom type passed to function as a parameter
问题
当我定义一个自定义类型时,似乎底层类型的不同会影响我是否可以直接将其传递给函数,还是需要进行转换。
问题是:
为什么 RuneFunc
和 StringMap
能够正常工作,但 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
时,它们可以成功运行。
英文:
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.
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) {
}
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.
答案1
得分: 22
Integer
和int
int
和你的新类型Integer
是两种不同的类型。在需要Integer
类型的地方,你必须传递一个Integer
类型的值。
如果你有一个Integer
值,你可以使用简单的类型转换将其转换为int
类型的值,因为Integer
的底层类型是int
:
var i Integer = 5
nf(int(i))
可能令人困惑和有趣的是,你可以在不进行转换的情况下传递一个无类型常量:
nf(5)
在Go Playground上尝试一下。
这是因为在规范: 可赋值性中有这样的规定:
值
x
可以在以下任何情况下赋值给类型T
的变量("x
可以赋值给T
"):
[...]
x
是一个无类型的常量,可以用类型T
的值表示。
5
是一个无类型常量,它的默认类型是int
,所以它可以用Integer
类型的值表示(因为Integer
的默认类型也是int
)。
如果你检查其他的可赋值性规则(不包括在上面的引述中),没有一个规则适用于你试图将Integer
类型的值传递给int
类型的参数,所以这是不允许的。
相关问题请参考:https://stackoverflow.com/questions/37385007/golang-creating-a-constant-type-and-restricting-the-types-values/37386119#37386119
RuneFunc
和func(rune) rune
这种情况与前面的情况(Integer
和int
)的不同之处在于,int
是一个命名类型,而func(rune) rune
不是。
并且有一个可赋值性规则允许这样做:
所以在这种情况下:
var f RuneFunc
ff(f)
f
是一个命名类型,但是ff()
的参数类型是func(rune) rune
,它是一个无名类型,所以赋值是允许的。
英文:
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
.
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)
Try these on the Go Playground.
The reason for this is in the Spec: Assignability:
> A value x
is assignable to a variable of type T
("x
is assignable to T
") in any of these cases:
> [...]
> - x
is an untyped constant representable by a value of type T
.
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).
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.
See related question: https://stackoverflow.com/questions/37385007/golang-creating-a-constant-type-and-restricting-the-types-values/37386119#37386119
RuneFunc
and 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 type V
and T
have identical underlying types and at least one of V
or T
is not a named type.
So in this case:
var f RuneFunc
ff(f)
f
is a named type, but the parameter type of ff()
is func(rune) rune
which is unnamed, so the assignment is allowed.
答案2
得分: 1
Go语言有严格的类型系统。仅仅因为你的类型只是int的别名,并不意味着你可以自由地互换它们,你仍然需要进行类型转换。下面是一个可工作的main函数版本,这是在playground上的代码链接:https://play.golang.org/p/BDTXnnG9Lg
英文:
Go has a strict type system. Just because your type is merely an alias for int doesn't mean you can interchange the two freely, you'll still have to do a type conversion. Below is a working version of your main, here's the code on play ground: https://play.golang.org/p/BDTXnnG9Lg
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论