A conversation with the Go team
blog.golang.org
blog.golang.org
Strange. Lack of generics is probably the single most common thing I read/hear people complain about.
(Btw, I like and use Go)
Lots of people have both tried Go, legitimately, and are still concerned about lack of Generics. I've read several that have done so, and have tried it myself and would like Generics. But sure, I could use it, and even enjoy it, even without Generics. That doesn't mean I'm perfectly happy with the situation.
[1] https://groups.google.com/forum/?fromgroups#!forum/golang-nu...
For example here is a recent thread on the list that quickly degenerated into an epic flamewar: https://groups.google.com/forum/?fromgroups=#!topic/golang-n.... (Most amusingly the OP's chosen handle is "Sod Almightly", a pretty strong signal that the guy is a troll, although props to Ian Taylor in particular for keeping civil throughout.)
He wrote more about this here: http://commandcenter.blogspot.com.es/2011/12/esmereldas-imag...
EDIT: Well, the answerer of that question was Robert (Griesemer), not Rob (Pike), but I suppose the same applies.
The classic example is a list (in a strongly-typed language) - without generics you would create specialised strongly-typed list classes - you could have a StringList (only strings), an IntList (only ints), etc.
Using generics you can define a single list class of type <T>, where <T> is any type. Then at runtime you would create a List<String> or a List<Int>. Same outcome, less code, easier to maintain.
MSDN has a good intro to C# generics - http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).as...
They are less appropriate in dynamic languages, as type-safety isn't a compile time concern.
Whether it's static or dynamic is language-dependent. Example: Java in particular does erasure on parameterized types, so generics are only compile-time.
No. Some languages implement it that way, but nothing about the concept involves moving anything to run time. In languages with reasonable type systems, there is no run time involvement at all.
What's interesting is that the .NET CLR directly supports parametric polymorphism. I think the .NET design is nicer than type erasure in Java but Java's approach is more applicable to situations where the runtime can't be modified. No idea how Go fits into that picture.
List := TList.Create();
Person := TPerson(List[0]);
With generics, you would have something like: List := TList<TPerson>.Create();
Person := List[0];
The TList<TPerson> class would be compiled at compile time and offer the same kind of type safety as, say, an array.In C++ it would be something like (simplified):
template <class T>
class TList {
private:
T[] elements;
public:
void add(T value);
void remove(T value);
T operator[](int index);
};
This extends to other declarations. For example, in C++ you can have generic functions. So you can have a functon max(a, b) which works on any type, for example.Generics is essentially about enhancing compile-time type safety and reusing code. By giving the type system more intelligence, more checks can be performed at compile time, and code can use the same logic across multiple types instead of having to be reimplemented for each type.
Incidentally, Delphi has had generics since 2007 [1]. I haven't used Delphi since version 5, so I haven't tried it out, but it looks fairly similar to the generics in C#.
[1] http://en.wikipedia.org/wiki/Generic_programming#Genericity_...