4 ms·
Are generics really that big of a thing if you got structural typing? I mean, you don't have to implement all the interfaces explicitly, you just have to get y
by k__ 10y ago
Are generics really that big of a thing if you got structural typing?
I mean, you don't have to implement all the interfaces explicitly, you just have to get your structure right and be done with it.
Am I missing something?
- buckhx 10y agoIt can be annoying to not have a generic collection. So if I build a heap I need to build it for a specific type or use interface{} as the values. That being said, it's never been a deal breaker for me and don't end up missing generics THAT much.
- whateveracct 10y agoYes they are. And actually they would play nice with generics! Imagine type Ord a interface { Compare(a) int } Also it would be nice to do something like func Max[a <: Ord a](a, a) a (Syntax stolen from Scala) Right now, interfaces are too opaque. You can't take a value of interface type X and return the same type. You have to return an opaque X, which gives you no guarantees about its concrete implementation. Parametricity is an extremely well-motivated and solved language feature! I think if you add parametricity to Go functions (not even parametric types. Just type variables that play nice with all the builtins) you can write this and get guarantees about its implementation (assuming it follows the functor laws) func map[a,b](func(a) b, []a) []b
- rbehrends 10y agoImplement a heap that works for any type with a user-defined ordering relation (in particular, you should be able to implement both max heaps and min heaps over the same type by changing the ordering). The heap should allow for efficient operations to add an element, retrieve the minimum element, and to merge two heaps; merging should be able to make use of specialized bulk operations rather than just adding elements one by one. The data structure should be opaque, so that you can (e.g.) switch out binary heaps for Fibonacci heaps later on. The interface should be typesafe. Heaps are useful, inter alia, to define efficient priority queues. Other examples: * Implement directed graphs using arbitrary types for nodes and edges. Graph algorithms are useful in a number of application areas. * Implement a parser combinator library that works for various types of tokens, semantic values, and states. General problems with lack of parametric polymorphism (where subtyping is not enough) are: * The necessity for the client of a service to cast the result to the desired type. * Difficulty in implementing binary operations efficiently where both operands are of the same or related types (such as the heap merge above), because you can't know for certain that they are of the same type just because they conform to the same interface. * Lack of type safety guarantees when you're mixing incompatible instances (such as merging a min heap and a max heap over the same type).
- jasonwatkinspdx 10y agoIt prevents anyone outside go core writing type safe containers.
- woah 10y agoI think generics would mostly be useful for container types, like putting methods on slices (like .map) without caring what's inside.
- nemo1618 10y agoYou might be interested in my side project: https://github.com/lukechampine/ply https://github.com/lukechampine/ply IMO when people say they want generics in Go, they really just want a few functional-style methods and functions on slices (e.g. map, filter, reduce methods, and functions like sort, reverse, repeat). So I wrote a compile-to-Go language that allow you to write "Go code" that also has those things. One of the things I've found, though, is that Go's first-class function syntax really harms the elegance of these sorts of constructions. For example, compare: // square a slice of ints in Go for x := range xs { x *= x } // square a slice of ints in Ply (morph == map) xs.morph(func(x int) int { return x*x }) When really, we want something like: xs.map(x => x*x) I think it's possible to achieve the latter without having to write a new lexer/parser/typechecker from scratch, but for now I'm avoiding adding any new syntax.
- woah 10y agoWouldn't xs.map((x int) int => x*x) be simple desugaring without messing with the type system?
- nemo1618 10y agoPerhaps, but you aren't saving a ton of typing if you still need to specify all the types. I think you can get away with xs.morph(x => x*x) because you know that xs is a slice of ints, so x must be an int, and that means x*x is an int, so the lambda has the right type signature. As far as I can see, there are two potential annoyances with this approach: you have to explicitly cast constants sometimes (e.g. uint64(3) instead of just 3), and you have to explicitly cast values if you want to return an interface (e.g. io.Writer(file) instead of just file). But these cases are rare enough that I think the tradeoff is worth it.