4 ms·
This looks like a good, minimal implementation of generics. It's mostly parameterized types. Go already had parameterized types, but only the built-ins - maps a
by Animats 6y ago
This looks like a good, minimal implementation of generics. It's mostly parameterized types. Go already had parameterized types, but only the built-ins - maps and channels. Like this.
var v chan int
Somewhat surprisingly, the new generic types use a different syntax:
var v Vector(int)
Do you now write
v := Vector(int)
or
v := make(Vector(int))
Not sure. The built-in generics now seem more special than they need to be. This is how languages acquire cruft.
The solution to parameterized types which reference built-in operators is similarly minimal. You can't define the built in operators for new types. That's probably a good thing. Operator overloading for non-mathematical purposes seldom ends well.
They stopped before the template system became Turing-complete (I think) which prevents people from getting carried away and trying to write programs that run at compile time.
Overall, code written with this should be readable. C++ templates have reached the "you are not supposed to understand this" point. Don't need to go there again.
- throwaway894345 6y agoPresumably you write v := Vector(int){} Or v := make(Vector(int)) Just like with unparameterized types.
- deleted 6y ago[deleted]
- kortex 6y agoIt's still fleshing out the details, and this is a great point to bring up. I suspect it may be a shim to make the parser more manageable. I for one would love to see all generics harmonize with the existing syntax for map, chan, etc.
- dvirsky 6y agoEven the current syntax is a bit non coherent. Would you go with "Vector[int]" a la map, or with "Vector int" a la chan? I think both considered the proposed syntax is actually better.
- nyanpasu64 6y agoIn my opinion, `Vector int` is the more logical syntax. `Vector[int]` to me seems like a vector indexed by int (rather than containing int), much like `map[int]T` is a map indexed by int, and `[5]T` is an array indexed by an integer less than 5. For the same reason, I dislike `std::array<T, 5>` which puts the range type before the domain constraint. This is inconsistent with `std::map<int, T>` which puts the domain type before the range type, and `[](int x) -> T` which puts the domain type before the range type.
- majewsky 6y ago> `Vector int` is the more logical syntax. This introduces several parser ambiguities, esp. related to nested template types or types with multiple parameters. For instance, does `Foo Bar Baz` read as `Foo(Bar, Baz)` or `Foo(Bar(Baz))`?
- Animats 6y agoRight. For consistency, the old built-in generics should be written var v chan(int) var m map(keytype,datatype) instead of var v chan int var m map[keytype]datatype but, for backwards compatibility, that probably won't happen. Thus, cruft. Map was always a bit too special in Go. Channels really are special; the compiler knows quite a bit about channels, what with "select", "case", and "go" keywords and their machinery. But maps are just a built in library object.
- marcus_holmes 6y agoI agree with your point about syntax - we're still not at the point where you could roll your own map-like type, and I think that should be a goal for this implementation. And yeah, operator overloading leads to utterly illegible code. Let's not go there!
- djur 6y agoWhat is missing for a "map-like type" other than operator overloading ([], []=)?
- marcus_holmes 6y agoI don't really understand the question. Is [] an operator?
- shoo 6y agoYes. "[]" would denote the operator for accessing element of collection associated with key/index k by [k] syntax. Similarly "[]=" would denote operator for storing element under a given key/index. E.g. in python these would correspond to a custom type implementing https://docs.python.org/3/reference/datamodel.html#object.__getitem__ https://docs.python.org/3/reference/datamodel.html#object.__... & https://docs.python.org/3/reference/datamodel.html#object.__setitem__ https://docs.python.org/3/reference/datamodel.html#object.__...
- marcus_holmes 6y agoOK, yeah, I get it now. so if I defined my own map type (e.g. orderedmap[T] that kept things in the order they were added) then I'd need to write code for the [] operator to access an element, is that right? I guess I'm not really sure how that differs from declaring orderedmap(T) (as it is in the proposal). Why do I need to overload one and not the other?
- uluyol 6y agoNot a strict requirement, but hashing pointer values. In Go, a pointer's integer value may not be stable since the GC may move the underlying data. This doesn't happen with today's runtime, but it may in the future. Why does this matter? What if I want to make a hash table with the key being a pointer? The built-in map can just go ahead and hash the pointer itself (i.e. a 32/64-bit integer) since it's a part of the runtime and will be updated if the GC is changed. But a user map cannot do this: it would have to hash the actual value itself. Depending on your data, this could be significantly more expensive. This doesn't stop anyone from implementing a hash table, but it may mean that it can't be as fast as the built-in map.