自定义类型作为参数传递 [英] Custom type passed to function as a parameter
问题描述
当我定义一个自定义类型时,似乎底层类型的类型会影响我是否可以将它传递给函数,或者我需要将其转换。
问题是:
为什么 RuneFunc
和 StringMap
工作,但不是整数
?
https://play.golang.org/p/buKNkrg5y-
包主类型
类型RuneFunc func(符文)符文
类型整型
类型StringMap map [string ] string
func main(){
// m:= make(StringMap)
// mf(m)
var i Integer = 5
nf(i)
// var f func(符文)符文
// ff(f)
func mf(i map [string] string){
}
func ff(i func(rune)rune){
}
func nf(i int){
}
$ b $在这里,当我使用 Integer
运行这个名为 nf
的函数时,它抱怨尽管 int
是基础类型。但是当我调用 mf
或 ff
时,它们会成功运行。
整数
和 int
int
和您的新类型整数
是2种不同的不同类型。其中 Integer
是预期的,您必须传递
如果你有一个 Integer
值,你可以使用一个简单的类型转换来使它成为 int
,因为 Integer
的基础类型是 int
:
var i Integer = 5
nf(int(i))
同时可能令人困惑和有趣的是,您可以在不转换的情况下传递无类型的常量:
nf(5)
请在 Go Playground 。
原因是规范:可分性:
x 是可赋值的给 T (
x
)可以赋值给<$ c $> =nofollow noreferrer c> T ):
[/ b]
x
是一个无类型的常量可以用类型T
的值表示。
5
是一个无类型常量,可以用类型 int
因为无类型常量 5
具有默认类型 a> int
,所以它可以用 Integer
类型的值表示(它具有相同的默认类型)。
如果您检查其他可分配性规则(不包括在上面的引用中),它们都不匹配您尝试传递整数值的情况为类型
int
的参数,所以不允许。
请参阅相关问题: Golang:创建常量类型并限制类型的值
RuneFunc
和 func(符文)rune
这种情况与前一个( Integer
和 int
)是 int
是命名类型和 func(符文)符文
不是。
有一个赋值规则允许这样做:
案例:
var f RuneFunc
ff(f)
$ b f
是一个命名类型,但是 ff()
是 func(符文)rune
这是未命名的,所以赋值是允许的。
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.
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 typeT
("x
is assignable toT
") in any of these cases:[...]
x
is an untyped constant representable by a value of typeT
.
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: Golang: Creating a Constant Type and Restricting the Type's Values
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 typeV
andT
have identical underlying types and at least one ofV
orT
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.
这篇关于自定义类型作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!