简体   繁体   English

为什么 Golang 允许两个具有不同接收者类型的函数具有相同的名称,但如果它们具有不同的参数类型则不允许?

[英]Why does Golang allow two functions to have the same name if they have different receiver types but not if they have different parameter types?

Why does Golang allow two functions to have the same name if they have different receiver types but not if they have different parameter types?为什么 Golang 允许两个具有不同接收者类型的函数具有相同的名称,但如果它们具有不同的参数类型则不允许? Consider for example考虑例如

type A struct{}
type B struct{}

// This is allowed
func (*A) foo(){}
func (*B) foo(){}

// This is not allowed
func foo(*A){}
func foo(*B){} // compilation error: "foo redeclared in this block"

What is the logic behind this choice?这个选择背后的逻辑是什么?

Disallowing methods of a concrete type with same name and different types is in the Go FAQ: Why does Go not support overloading of methods and operators? Go 常见问题解答:为什么 Go 不支持方法和运算符的重载?

Method dispatch is simplified if it doesn't need to do type matching as well.如果方法分派也不需要进行类型匹配,则它会得到简化。 Experience with other languages told us that having a variety of methods with the same name but different signatures was occasionally useful but that it could also be confusing and fragile in practice.使用其他语言的经验告诉我们,具有相同名称但不同签名的各种方法偶尔有用,但在实践中也可能令人困惑和脆弱。 Matching only by name and requiring consistency in the types was a major simplifying decision in Go's type system.仅按名称匹配并要求类型一致是 Go 类型系统中的一个主要简化决策。

Regarding operator overloading, it seems more a convenience than an absolute requirement.关于运算符重载,它似乎比绝对要求更方便。 Again, things are simpler without it.同样,没有它,事情会更简单。

Allowing methods with same name of different types (with optionally identical parameter types, declared in the same package) is allowed for obvious reasons: bounding methods to a type naturally gives the method a "name space", the type they belong to.允许具有相同名称的不同类型的方法(具有可选的相同参数类型,在同一包中声明)是允许的,原因很明显:将方法绑定到一个类型自然地为该方法提供了一个“名称空间”,即它们所属的类型。

If it would not be allowed, you could not declare multiple types in the same package that would implement the same interface(s).如果不允许,则不能在实现相同接口的相同 package 中声明多个类型。 That would require putting them into different packages.这将需要将它们放入不同的包中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 两个相似的函数如何在Haskell中具有不同的多态类型? - How can two similar functions have different polymorphic types in Haskell? 如何为同一个功能使用不同的输入类型? - How to have different input types for the same function? 为什么具有相同`id`的两个函数具有不同的属性? - Why can two functions with the same `id` have different attributes? JavaScript有function个参数类型吗? - Does JavaScript have function parameter types? JavaScript为什么getItem()有两种返回类型? - JavaScript Why does getItem() have two return types? 我可以使用lambda指针数组来保存具有不同类型的参数/返回值的函数吗? - Can I have an array of lambda pointers in order to hold functions with different types of arguments/return values? 有没有办法让 Typescript 在具有不同参数计数时考虑函数类型不等价? - Is there a way to make Typescript consider function types non-equivalent when they have different parameter counts? gcc:为什么两个不同的变量在调用后具有相同的值? - gcc: Why does two different variables have the same value after call? 为什么两个函数具有相同的地址? - Why do two functions have the same address? 我可以快速设置两种类型的参数吗? - Can i have 2 types on parameter in swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM