Generic type

from Wikipedia, the free encyclopedia

In computer science , generic types are data types with the option of specifying type parameters. One also speaks of parametric polymorphism .

A generic type allows data types to be created that abstract from the underlying types. So one can program a list of numbers, a list of characters, and a list of dates in the same way; H. the algorithms for inserting, searching and deleting would always run the same. It is therefore desirable to implement the list independently of these types.

For example, in Java a generic list would be defined as follows:

 List<T>

Here means List<T> generic type , and Tis called type parameter . Generic types are found in all programming languages that the paradigm of generic programming support.

In programming languages, there are basically two approaches to supporting generic types. One is that the same code is used GenerischerTyp<T>for all Ts and uses specific properties of via dynamic linkage of methods T. Another way u. a. C ++ is all about using templates . The type parameter is replaced directly with the specific type at compile time. If there are several different type parameters for a generic type, several classes are also created in C ++.

In .NET is Microsoft a middle ground to take advantage of combining both methods: For all reference types as type parameters, the generic class uses the same native code because the internal representation (a simple reference ) of all reference types is the same. When using value types as type parameters, a separate native code path is created for each value type at runtime when it is used for the first time.

Generic types in various programming languages

  • C ++ implements generic types using templates, e.g. B. Class templates.
  • Generic programming in Java
  • C # and other .NET languages ​​have offered generics since .NET 2.0
  • In the Ada programming language , generic types are used to parameterize packages.
  • Delphi has offered support for generic types since Delphi 2009, Free Pascal supports generic classes from version 2.2.
  • The Haxe programming language offers generic types for Flash, JavaScript and server-side execution (all are three areas in which generics are otherwise not possible)
  • Haskell offers a very powerful type system