Generic Types

A generic type is one programming construct that adapts to do the same operations for many data types. When you write generic code, it is not necessary to write different implementations for each possible data type.

Example generic class

Class Container[T]
    Method Add(item As T)
        Dim temp As T
    End Method
End Class

Permitted generic constructs

You can make and use generic classes, structures, traits, and procedures. The ViviFire standard libraries supply many frequently used generic constructs that you can use in your code.

Although procedures are not types, you can make and use generic procedures. See Generic Procedures for more information.

How generic types help

You use generic types to declare some different programming elements, where each one operates on the specified data types. The alternatives to a generic type are:

Generic types are better than these alternatives because of:


Example of a constraint


Types of constraints

There are five types of constraints.

If you must specify more than one requirement, then you can put a comma between each one. Or you can use Where again and again, each on a different line.

See Where Clause (Generics) for more information.

Examples of many constraints

Class MyClass[T] Where T Is Class Does Compare Constructor()
    ' ...
End Class
Class MyClass[T]
    Where T Is Class, T Does Compare, T Constructor()
    ' ...
End Class
Class MyClass[T]
    Where T Is Class
    Where T Does Compare
    Where T Constructor()
    ' ...
End Class