Restrictions of user-defined generics
- Last Updated: May 22, 2024
- 3 minute read
- OpenEdge
- Version 13.0
- Documentation
There are restrictions with user-defined generics in regard to the following:
- .NET objects
CATCHstatementENUMstatementTYPE-OFfunctionCASTfunctionGET-CLASSfunctionNEWstatement- STATIC members
The details are described in the following sections.
.NET objects
.NET objects are not allowed as type parameters or type arguments. For example:
|
CATCH statement
The CATCH statement does not allow a generic type or a type
parameter. For example, you get a compiler error if you write CATCH
statements like the following:
|
ENUM statement
ENUM types cannot be generic.
TYPE-OF and CAST functions
You cannot use a type parameter by itself in the generic class implementation where a
type would be specified for the TYPE-OF() and
CAST() built-in functions. For example:
|
In such cases, you can use the constraint type, instead of T. For
example:
|
GET-CLASS function
GET-CLASS() built-in function. For
example:
|
NEW statement
TheNEW statement
does not allow the type parameter. The compiler cannot perform checking on the signature
of the constructor since the real type is not known until the client of the generic
class specifies the type argument. For example:
|
You can use dynamic programming and/or reflection to perform tasks that
are dependent on the type of the type argument specified by the client of the
generic type. Progress.Lang.Class contains the
GetTypeArguments() method, which you can use to get the type of
the type parameters. For example:
|
myListIterator is Progress.Lang.Object, the
NEW statement is allowed because the type constraint for
T is Progress.Lang.Object:
|
The following is a more complex hierarchical example which does compile:
|
In the example above, mySimpleGeneric has a type
parameter with the myBaseClass type constraint. Since the type
parameter constraint in DerivedGeneric is
myDerivedClass, the variable declaration in
somemeth is valid, as myDerivedClass is a
subclass of myBaseClass. If the types are not compatible, the
compiler raises an error.
In summary, a type parameter may only be referenced
in built-in functions TYPE-OF, CAST,
GET-CLASS, and NEW if it is used as a type
argument to a generic type and it follows the type compatibility rules.
STATIC members
Generic classes themselves are not allowed to define STATIC members:
methods, variables, properties, events, and so on. It is valid to have static
members in a non-generic class in the hierarchy, but the generic type itself cannot
have static members.
However, you can access inherited static members using the TYPE-NAME
syntax using the generic type, but understand that they are in the non-generic
class. A clearer way to reference them is using the actual type that defines them.
For example, given these classes:
|
You can write either one of these statements, but the latter is clearer:
|