Re:A conversation with the Go team
by lobster_johnson on Thursday June 06, 2013 @10:12AM
(#5832503)
Example of generics: In Delphi, you have TList. It stores any TObject, but it's not type safe, so if you store something like TPerson in it, you have to cast it every time:
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#.
In C++ it would be something like (simplified):
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_...