4 ms·
Sorry what's a generic? Can you explain it in Delphi 6 terms? er actually Python terms would be even better.
by tocomment 13y ago
Sorry what's a generic? Can you explain it in Delphi 6 terms? er actually Python terms would be even better.
- deleted 13y ago[deleted]
- tocomment 13y agoWhat's so good about that? Isn't it more convenient to be able to store different types together in the same collection?
- sluukkonen 13y agoIt can be more convenient, but the added type safety prevents the programmer from doing many kinds of common mistakes. It's a trade-off.
- coldtea 13y agoNo. You typically want different types of the same kind, or of the same family (e.g numeric types or types that extend some interface, etc), not every random thing thrown in. If it was truly whatever type, how would you know what to do with it when you got it out of the collection? E.g foo = myColection.get(2); foo.?????? (what operations can foo afford?)
- hugorodgerbrown 13y agoGenerics are used to defer type setting from compile time to run time, allowing for flexible, yet type-safe constructs. 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).aspx 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.
- throwit1979 13y agoRuntime? Huh? Whether it's static or dynamic is language-dependent. Example: Java in particular does erasure on parameterized types, so generics are only compile-time.
- papsosouid 13y ago>Generics are used to defer type setting from compile time to run time, allowing for flexible, yet type-safe constructs. 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.
- zwieback 13y agoHere's another good paper from MS on generics in .NET and C#: http://research.microsoft.com/pubs/64031/designandimplementationofgenerics.pdf http://research.microsoft.com/pubs/64031/designandimplementa... 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.
- lobster_johnson 13y agoExample 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#. [1] http://en.wikipedia.org/wiki/Generic_programming#Genericity_in_Delphi http://en.wikipedia.org/wiki/Generic_programming#Genericity_...