How is the empty interface different than a generic?

huangapple go评论75阅读模式
英文:

How is the empty interface different than a generic?

问题

也许我对泛型的威力还不够了解,但空接口interface{}与泛型有什么不同,特别是如果我们可以使用反射或类型切换的能力?人们总是提到Go语言没有泛型,但interface{}似乎可以很好地完成类似Java中的<T>的工作。

英文:

Maybe I'm not fully versed on the power of generics, but how is the empty interface, interface{}, different than a generic, especially if we have the ability to use reflection or type switches? People always mention that Go doesn't have generics, but interface{} seems like it does the job pretty comparable to something like &lt;T&gt; in Java.

答案1

得分: 14

如果你来自Java,空接口(interface{})实际上比泛型更接近于在Java中使用Object变量。

你可以将任何东西赋值给interface{}(就像在Java中可以对Object变量做的那样)。

但是如果你想使用存储在其中的实际类型,你应该进行“类型断言”(type assert)或“类型转换”(cast)(与在Java中处理Object变量时需要做的相同操作)。

Java中的泛型与此有很大不同,因为它们允许你在编译时进行类型检查。区别在于,如果你使用泛型,就不需要使用反射或类型切换。

你可以在这里阅读更多关于Java泛型的内容:

https://docs.oracle.com/javase/tutorial/java/generics/

然后,你可以在这里继续阅读关于空接口的内容,包括接下来的2或3个步骤:

https://tour.golang.org/methods/14

英文:

If you come from Java, the empty interface (interface{}) is actually closer to working with Object variables in Java than with generics.

You can assign anything to an interface{} (like you can do with an Object variable in Java).

But you should then "cast" or "type assert" back if you want to use the actual type you stored there (same that you need to do with Object variables in Java).

Generics in Java are quite different, since they allow you to keep type checking at compile time. The difference is precisely that you don't need to resort to reflection or type switches if you work with Generics.

You can read more about Java generics here:

https://docs.oracle.com/javase/tutorial/java/generics/

And then follow this and the next 2 or 3 steps of the Go tour here for more on how the empty interface works:

https://tour.golang.org/methods/14

答案2

得分: 7

考虑到泛型的主要目的是在提供写类型不可知函数/方法的同时,保持编译时类型安全检查,使用带有运行时类型断言/切换的空接口与泛型完全不同,我认为从编程范式的角度来看,它几乎是泛型的完全相反。

我认为过去十年中超过一半的编程语言改进都是为了避免运行时错误,我猜这也是为什么Go语言有一些"内置泛型",比如切片和映射,而不是像旧版JavaScript的数组那样只在运行时对元素进行类型检查。因此,在Go语言中,使用带有类型断言/切换的空接口绝对不能替代泛型,个人而言,我会尽量避免使用空接口。

英文:

Considering the main point of generics is to maintain the compile-time type safety check for statically typed languages when providing facilities to write type agnostic functions/methods, the empty interface with runtime type assertions/switches is completely different from generics and I'd say it's almost the complete opposite to generics in terms of programming paradigms.

I'd say more than half of the programming language improvements over the last decade are about avoiding runtime errors, and I guess that's why Go has some "built-in generics" like slice and map instead of something like the old JavaScript's Array stuff which only has type checks on its elements during run-time. So the empty interface with type assertion/switch in Go is definitely no replacement for generics and personally, I'd try to avoid using the empty interface as much as possible.

huangapple
  • 本文由 发表于 2017年7月18日 23:48:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/45171642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定