ジェネリック USING 指定

ジェネリックのタイプとメソッドは、COBOL で USING 指定を使用して定義できます。

generic-using-phrase

generic-argument

generic-argument

generic-argument には、従来、T や U などの単一の大文字があります。

この例では、メイン メソッドは、2 つのパラメーターよりも大きい数字と小さい数字を見つけて表示します。T は Min メソッド (この場合は静的ジェネリック メソッド) に対するジェネリック引数です。Min メソッド は、2 つのパラメーター (どちらもジェネリック パラメーターと同じタイプのパラメーター) を予測します。T は IComparable インターフェイスを実装することがわかっているため、CompareTo メソッドを呼び出すことができます。

       class-id GenericMath.

       method-id main static.
       01 n1 binary-double.
       01 n2 binary-double.
           set n1 to 999
           set n2 to 100
           display n1 space n2
           display Min[binary-double](n1 n2)
           display Max[binary-double](n1 n2)
           goback.
       end method.
       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.
       method-id Max 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 item2
           else
               set res to item1
           end-if
       end method.

       end class.