Previous Topic Next topic Print topic


Constraints Paragraph

The Constraints paragraph allows specification of properties of parameters of a generic type or method, and therefore allows the use of methods or properties on data items whose type is specified as that parameter.

constraints-paragraph

type-specifier type-specifier

Example

The following shows a static generic method with a USING phrase that declares the generic argument T. This generic argument is constrained as the type System.IComparable. The method expects two parameters both of which must be of the same type as the generic argument.

       method-id Min using T static.
       constraints.
           constrain T implements type System.IComparable.
       procedure division using by value item1 as T item2 as T
                      returning res as T.
           if item1::CompareTo(item2) < 0
               set res to item1
           else
               set res to item2
           end-if
       end method.

In this case the CompareTo method may be invoked since T is known to implement the IComparable interface.

Previous Topic Next topic Print topic