6 ms·
> What's the point of interface{}, then, if it doesn't tell us anything about the value? Well, that's precisely why it's useful: it can refer to anything! An e
by nixpulvis 5y ago
> What's the point of interface{}, then, if it doesn't tell us anything about the value? Well, that's precisely why it's useful: it can refer to anything!
An empty interface should be effectively useless. What could it possibly do!? This pattern is much more like a dynamic container with a type tag.
- tptacek 5y agoDoing nothing is the point; it serves the same purpose as a `void*` in C, except that there are (runtime-) safe ways to take a value out of `interface{}` and back to its concrete type in Go. The most obvious use for it is heterogenous containers.
- nixpulvis 5y agoRuntime safe and statically type checked are two fundamentally different things is my point. Interfaces in most typed languages are static concepts, though there are always exceptions.
- strbean 5y agoYou can think of interface{} like Object in Java. Interfaces in go are simply saying "this thing satifies these properties". interface{} is an interface that anything can satisfy.
- nixpulvis 5y agoI will continue to think of interface{} as Any in Rust.
- ibraheemdev 5y ago`interface{}` used as a type is almost exactly `Box<dyn Any>`.
- throw_m239339 5y ago> The most obvious use for it is heterogenous containers. You mean runtime generic container without the benefits of compile-time type checking, opening the developer to runtime type errors purely for ideological reasons...
- a1369209993 5y ago> opening the developer to runtime type errors purely for ideological reasons... To be fair, there are also language-implementor-laziness reasons.
- earthboundkid 5y agoThat Go would benefit from generics doesn't change the fact that there are also lots of use for heterogenous containers. As it is, Go already has "slices" for type safe containers. The fair complaint about Go is it's hard to do other type safe container than the built in "slice" and "map" containers. But the problem is on track to be fixed next year, so it's silly to complain about it now.
- earthboundkid 5y agoThat Go would benefit from generics doesn't change the fact that there are also lots of use for heterogenous containers. As it is, Go already has "slices" for type safe containers. The fair complaint about Go is it's hard to do other type safe container than the built in "slice" and "map" containers. But the problem is on track to be fixed next year, so it's silly to complain about it now. It's really hard to take people who complain about Go seriously when they're pretty obviously just zealots who don't know much about the language besides "types good" and "Go bad". My stance: types good in moderation; Go a useful tradeoff of engineering attributes.
- throw_m239339 5y ago> so it's silly to complain about it now. No it isn't, Go has other flaws stemming from the exact same root cause and it has nothing to do with programming...
- tptacek 5y ago
- jrockway 5y agoI don't think "empty" is quite the right mental model. It's an object that has at least 0 methods, which is a characteristic of every data type. An actually empty type in Go is struct{} (good for map values when the map represents a set).
- LambdaComplex 5y agoTo be clear, "empty interface" is the official term: ctrl+f "empty interface" on https://golang.org/ref/spec https://golang.org/ref/spec
- ibraheemdev 5y agoWell the interface's method set is empty, not the underlying type.
- jhoechtl 5y agoThis article would be great to be revised once parametric types (AKA "generics") become an official part of the language. https://go.googlesource.com/proposal/+/master/design/15292/2013-12-type-params.md https://go.googlesource.com/proposal/+/master/design/15292/2... https://go2goplay.golang.org/ https://go2goplay.golang.org/
- kjksf 5y agomap in Go is already generic. If you need map[string]int or map[int]float, you can already do it. Generics do not obviate the need for interface{} or the described use case of map[string]interface{} interface{} is like Java or C# object. It packages a type and a value in a single value that can be queried at runtime to extract the underlying value in a type safe matter. When people use map[string]interface{] it's because they don't know the type of value at compile time.
- cle 5y agoI'm not sure why you're being downvoted, the only alternatives I've seen are to use methods or enums, which constrain the types to those supported by JSON, but ultimately you still have to switch on the type at runtime, the tradeoff being between closed enums vs. open interfaces, or the runtime tradeoffs of methods. Rust's serde_json uses enums for this, but the maps are generic in the same way that they already are in Go.
- ibraheemdev 5y ago> the maps are generic in the same way that they already are in Go. Go maps are actually not generic in the same way they are in other languages, there is just a single map struct with type descriptors used to do some compiler magic: https://dave.cheney.net/2018/05/29/how-the-go-runtime-implements-maps-efficiently-without-generics https://dave.cheney.net/2018/05/29/how-the-go-runtime-implem...
- deleted 5y ago[deleted]