Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the empty interface different than a generic?

Tags:

generics

go

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 <T> in Java.

like image 946
ollien Avatar asked Jul 18 '17 15:07

ollien


People also ask

What is the point of an empty interface?

An empty interface in Java is known as a marker interface i.e. it does not contain any methods or fields by implementing these interfaces a class will exhibit a special behavior with respect to the interface implemented. java. lang. Cloneable and java.

Why do we need empty interface in Java?

Implementing an empty interface tells the compiler to do some operations. It is used to logically divide the code and a good way to categorize code. It is more useful for developing API and in frameworks like Spring.

How is a generic interface defined?

A generic interface is primarily a normal interface like any other. It can be used to declare a variable but assigned the appropriate class. It can be returned from a method. It can be passed as argument.

When would you use a generic interface?

It's often useful to define interfaces either for generic collection classes, or for the generic classes that represent items in the collection. To avoid boxing and unboxing operations on value types, it's better to use generic interfaces, such as IComparable<T>, on generic classes.

What is a generic interface?

This article provides an overview of generic interfaces that provide common functionality across families of generic types. Generic interfaces provide type-safe counterparts to nongeneric interfaces for ordering and equality comparisons and for functionality that is shared by generic collection types.

What is an empty interface?

Here is a good definition of the empty interface by Jordan Oreilli: An interface is two things: it is a set of methods, but it is also a type. The interface {} type is the interface that has no methods.

Can we replace generics with empty interfaces in Golang?

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. To add to your excellent answer. Golang now provides experimental support for type parameters for generic programming.

Can an interface have more than one type parameter?

An interface can define more than one type parameter, as follows: The rules of inheritance that apply to classes also apply to interfaces: Generic interfaces can inherit from non-generic interfaces if the generic interface is contravariant, which means it only uses its type parameter as a return value.


2 Answers

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.

like image 158
hellopeach Avatar answered Oct 22 '22 21:10

hellopeach


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

like image 27
eugenioy Avatar answered Oct 22 '22 19:10

eugenioy