为什么我可以输入别名函数并使用它们而不用转换? [英] Why can I type alias functions and use them without casting?
问题描述
在Go中,如果您定义一个新类型,例如:
In Go, if you define a new type e.g.:
type MyInt int
然后,您不能将 MyInt
传递给需要int的函数反之:
You can't then pass a MyInt
to a function expecting an int, or vice versa:
func test(i MyInt) {
//do something with i
}
func main() {
anInt := 0
test(anInt) //doesn't work, int is not of type MyInt
}
很好。但为什么它不是同样不适用于函数?例如:
Fine. But why is it then that the same does not apply to functions? e.g.:
type MyFunc func(i int)
func (m MyFunc) Run(i int) {
m(i)
}
func run(f MyFunc, i int) {
f.Run(i)
}
func main() {
var newfunc func(int) //explicit declaration
newfunc = func(i int) {
fmt.Println(i)
}
run(newfunc, 10) //works just fine, even though types seem to differ
}
现在,我不抱怨,因为它节省了我不得不显式转换 newfunc
键入 MyFunc
我将在第一个例子中做;它只是似乎不一致。我相信有一个很好的理由;任何人都可以启发我?
Now, I'm not complaining because it saves me having to explicitly cast newfunc
to type MyFunc
, as I would have to do in the first example; it just seems inconsistent. I'm sure there is a good reason for it; can anyone enlighten me?
我问的原因主要是因为我想以这种方式缩短一些相当长的函数类型,但我想确保它预期和可接受的做到这一点:)
The reason I ask is mainly because I would like to shorten some of my rather long function types in this way, but I want to make sure it's expected and acceptable to do this :)
推荐答案
结果,这是一个误会,这可以通过阅读规范的相关部分来解决:
Turns out, this is a misunderstanding that I had about how Go dealt with types, which can be resolved by reading the relevant part of the spec:
http: //golang.org/ref/spec#Type_identity
我不知道的相关区别是命名和未命名类型。
The relevant distinction that I was unaware of was that of named and unnamed types.
命名类型是具有名称的类型,例如int,int64,float, bool。此外,使用'type'创建的任何类型都是命名类型。
Named types are types with a name, such as int, int64, float, string, bool. In addition, any type you create using 'type' is a named type.
未命名类型是[] string,map [ string] string,[4] int。它们没有名称,只是对应于它们的结构的描述。
Unnamed types are those such as []string, map[string]string, [4]int. They have no name, simply a description corresponding to how they are to be structured.
如果比较两个命名类型,则名称必须匹配才能互换。如果您比较已命名和未命名的类型,则只要基本表示符合,就可以了!
If you compare two named types, the names must match in order for them to be interchangeable. If you compare a named and an unnamed type, then as long as the underlying representation matches, you're good to go!
给定以下类型:
type MyInt int
type MyMap map[int]int
type MySlice []int
type MyFunc func(int)
以下内容无效:
var i int = 2
var i2 MyInt = 4
i = i2 //both named (int and MyInt) and names don't match, so invalid
以下是正确的:
is := make([]int)
m := make(map[int]int)
f := func(i int){}
//OK: comparing named and unnamed type, and underlying representation
//is the same:
func doSlice(input MySlice){...}
doSlice(is)
func doMap(input MyMap){...}
doMap(m)
func doFunc(input MyFunc){...}
doFunc(f)
我有点不懂我不知道更快,所以我希望澄清类型lark一点别人!并且意味着比我第一次想到的更少的投射:)
I'm a bit gutted I didn't know that sooner, so I hope that clarifies the type lark a little for someone else! And means much less casting than I at first thought :)
这篇关于为什么我可以输入别名函数并使用它们而不用转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!