Method Overloading - COBOLStandard.info

advertisement
24 January 2005
Document Number:
J4/05-0009
Page
Subject:
Method Overloading
Author:
Robert A. Karlin
1 of 10
References:
1.
Base Document: ISO/IEC 1989:20xx, November 2003
2.
04-0085 - Combined change proposal #1
3.
04-0201 – Method Overloading
PROPOSED REVISION:
Add the facility for Method Overloading.
JUSTIFICATION:
Method overloading, also called parametric polymorphism allows methods to be defined with the same
name and differing parameters within the same class. Many major OO languages implement this
feature, including C++ and Java. This feature becomes necessary to appropriately interface with these
languages.
COMPATIBILITY:
This feature is compatible with ISO/IEC 1989:2002.
DISCUSSION:
Initial Investigation
Extending COBOL to allow method overloading involves changing only the definitions of method
resolution. However, this is not as easy as in other languages, due to COBOL’s flexibility in automatic
parameter type conversions. For parameters passed by reference, choosing between methods is
simple, however parameters passed by content or value need not correspond exactly to the formal
parameters. Passing the literal ‘9’ is legal for a field defined with a formal parameter of pic X, pic XX, pic
9, pic P, pic Binary-Long, etc.
Note that we do not want to change any of the rules of object compliance. Compliant objects must still
follow all of the rules as they do now. We don’t care about how interfaces match, or inheritance takes
place. We are only concerned with the resolution of the method in the procedure division.
Meeting 249
1) Added language to handle optional parameters, so that optional parameters that correspond to
omitted parameters in the invocation are always considered to be an exact match.
24 January 2005
Document Number:
J4/05-0009
Page
2 of 10
2) Corrected the matching of parameters that are defined with the ACTIVE CLASS phrase in the
invoked method.
3) Gave tiebreaker preference to matches where the returned value is an exact match.
4) Added language to describe matching of numeric literals.
PROPOSED CHANGES TO BASE DOCUMENT:
1. Page 103, 8.4,5,4, Scope of method names:
1) Paragraph 2 sentence 1, replace in part “…unique method-names …” with “… unique
method resolution signatures …”
2) Paragraph 2 sentence 2, replace in part “… the same name …” with “… the same
method resolution signature …”
3) Paragraph 3 sentence 1, replace in part “…unique method-names …” with “… unique
method resolution signatures …”
4) Paragraph 3 sentence 2, replace in part “… the same name …” with “… the same
method resolution signature …”
2. Page 159, add new section(s) after 9.3.4, Methods:
”
9.3.4a
Polymorphism[Polymorphism]
Polymorphism is a feature of object orientation that allows a given method to do different
things. COBOL supports three types of polymorphism, class polymorphism, ad-hoc
polymorphism and parametric polymorphism.
9.3.4a.1
Class polymorphism [Class polymorphism][Polymorphism:class]
In COBOL, the ability for an object reference to contain references to objects of different
classes allows a method invocation on that object reference to be bound to one of many
possible methods. Sometimes the method can be identified before execution, but in general,
the method cannot be identified until runtime.
A data item can be declared to contain references to objects of a given class or any sub-class
of that class; it can also be declared to contain references to objects that implement a given
interface. When a given interface is used, the classes of the objects may be completely
unrelated, as long as they implement the given interface.
9.3.4a.2
Ad-hoc polymorphism [Ad-hoc polymorphism][Polymorphism:ad-hoc]
Ad-hoc polymorphism allows a method to respond to different types of parameters at runtime.
COBOL provides limited support for ad-hoc polymorphism through class conditions, described
in 8.8.4.1.3 Class conditions, and use of the redefines clause. Through these mechanisms a
method can differentiate and respond to any class of data that is not implicitly or explicitly
strongly typed.
9.3.4a.3
Parametric polymorphism [Parametric polymorphism][Polymorphism:parametric]
24 January 2005
Document Number:
J4/05-0009
Page
3 of 10
Parametric polymorphism allows methods within a class to have the same method name,
differing in the number and type of parameters. The most appropriate method for any
particular invocation is chosen during method resolution. Each method has a method
resolution signature that is matched against the parameters of the invocation. The method
resolution signature contains all of the information necessary to resolve the identity of the
method invoked. [Method resolution signature:definition][Signature:method:resolution] The
method resolution signature consists of:
1)
2)
3)
4)
5)
6)
The method name
The expected calling convention
The DECIMAL-POINT IS COMMA clause
The CURRENCY SIGN clause
The LOCALE clause
A description of each parameter, and of the returning item. This description consists of
the following clauses:
a) ANY-LENGTH clause
b) BLANK WHEN ZERO clause
c) JUSTIFIED clause
d) PICTURE clause
e) SIGN clause
f) USAGE clause
”
3. Page 159, 9.3.5, Method Invocation, replace from “Method resolution proceeds…” thru end of
section
” Method resolution proceeds as follows:
1) if a method with the method-name specified in the invocation is defined in the class of
the object and that method is an exact match between the method invocation and the
method resolution signature as specified below, that method is bound;
2) otherwise, each inherited class upward in the hierarchy of inheritance is inspected in left
to right order as written in the INHERITS clause until either a method with the methodname specified in the invocation is defined in the inspected class and is an exact
match, or all inherited classes have been inspected without finding such a method. If a
method is found, that method is bound;
3) otherwise, if one or more methods with the method-name specified in the invocation is
defined in the class of the object and one or more of those methods match between the
method invocation and the method resolution signature, that method is bound as
specified below;
4) otherwise, each inherited class upward in the hierarchy of inheritance is inspected in left
to right order as written in the INHERITS clause until either one or more methods with
the method-name specified in the invocation is defined in the inspected class and one
or more of those methods match between the method invocation and the method
resolution signature, or all inherited classes have been inspected without finding such a
method. If a method is found, that method is bound;
5) otherwise, the EC-OO-METHOD exception condition is set to exist.
24 January 2005
Document Number:
J4/05-0009
Page
4 of 10
For a method invocation to match the method resolution signature of a method, the following
shall be true:
1) The number of invocation parameters shall be equal to the number of parameters
defined in the invoked method. If the number of parameters in the invocation is less
than the number of parameters in the invoked method, and all parameters to the right of
the parameter in the position that corresponds to the number of parameters in the
invocation are described with the OPTIONAL phrase, the invocation and the invoked
method shall be considered to have an equal number of parameters. If there is a
returning item in the invocation there shall be a returning item in the invoked method, if
there is no returning item in the invocation there shall be no returning item in the
invoked method.
2) The same calling convention shall be in effect for the invocation as is in effect for the
invoked method.
3) For each parameter of the invocation that is passed by reference there must be a
corresponding parameter in the invoked method that:
a) is declared with the BY REFERENCE phrase
b) if the passed parameter is declared with the OMITTED phrase, must be
declared with the OPTIONAL phrase. No further checking is performed on
this parameter and this parameter is considered to match exactly.
c) is the same class and category
d) if the parameter in the invoked method is an object reference, the
corresponding parameter must follow these rules:
1. If the parameter in the invoked method is a universal object reference,
the corresponding parameter in the invocation is a universal object
reference.
2. If the parameter in the invoked method is described with an interfacename, the corresponding parameter in is described with the same
interface- name.
3. If the parameter in the invoked method is described with a classname, the corresponding parameter in the invocation is described with
the same class-name, and the presence or absence of the FACTORY
and ONLY phrases is the same in both descriptions.
4. If the parameter in the invoked method is described with the ACTIVECLASS phrase and FACTORY phrase is not specified, the
corresponding parameter shall evaluate to an object reference of the
same class specified in the invocation.
5. If the parameter in the invoked method is described with the ACTIVECLASS and FACTORY phrases, the corresponding parameter shall
evaluate to an object reference to the factory of the class specified in
the invocation.
24 January 2005
Document Number:
J4/05-0009
Page
5 of 10
e) if the parameter in the invoked method is not an object reference, the
corresponding parameter has the same ANY LENGTH, BLANK WHEN
ZERO, JUSTIFIED, PICTURE, SIGN, and USAGE clauses, with the following
additional constraints:
1. Currency symbols match if and only if the corresponding currency
strings are the same.
2. Period picture symbols match if and only if the DECIMAL-POINT IS
COMMA clause is in effect for both or for neither of these interfaces.
Comma picture symbols match if and only if the DECIMAL-POINT IS
COMMA clause is in effect for both or for neither of these interfaces.
Locale specifications in the PICTURE clauses match if and only if:
─
both specify the same SIZE phrase in the LOCALE phrase of the
PICTURE clause, and
─
both specify the LOCALE phrase without a locale-name or both
specify the LOCALE phrase with the same external identification,
where the external identification is the external-locale-name or literal
value associated with the locale-name in the LOCALE clause of the
SPECIAL-NAMES paragraph.
4) For each parameter that is passed by content there must be a corresponding parameter
in the invoked method that:
a) is declared with the BY REFERENCE phrase
b) if the passed parameter is declared with the OMITTED phrase, must be
declared with the OPTIONAL phrase. No further checking is performed on
this parameter and this parameter is considered to match exactly..
c) if the parameter in the invoked method is class OBJECT, POINTER, or
INDEX, may be a receiving field in an SET statement with the parameter of
the invocation as a sending field. If the sending field is a literal, the SET
statement must not cause truncation.
d) if the parameter in the invoked method is not class OBJECT, POINTER, or
INDEX, may be a receiving field in an MOVE statement with the parameter of
the invocation as a sending field. If the sending field is a literal, the MOVE
statement must not cause truncation.
5) For each parameter that is passed by value there must be a corresponding parameter
in the invoked method that:
a) is declared with the BY VALUE phrase
b) if the passed parameter is declared with the OMITTED phrase, must be
declared with the OPTIONAL phrase. No further checking is performed on
this parameter and this parameter is considered to match exactly..
24 January 2005
Document Number:
J4/05-0009
Page
6 of 10
c) if the parameter in the invoked method is class OBJECT, POINTER, or
INDEX, may be a receiving field in an SET statement with the parameter of
the invocation as a sending field. If the sending field is a literal, the SET
statement must not cause truncation.
d) if the parameter in the invoked method is not class OBJECT, POINTER, or
INDEX, may be a receiving field in an MOVE statement with the parameter of
the invocation as a sending field. If the sending field is a literal, the MOVE
statement must not cause truncation.
6) If the invocation specifies a returning item and the returning item is usage OBJECT
REFERENCE, POINTER or INDEX, there must be a corresponding specification in the
invoked method that may be a sending item in a SET statement with the returning item
as the receiving item.
7) If the invocation specifies a returning item and the returning item is not usage OBJECT
REFERENCE, POINTER or INDEX, there must be a corresponding specification in the
invoked method that may be a sending item in a MOVE statement with the returning
item as the receiving item.
Additionally, for each parameter that is passed by value or by content, and for the returning
item, the following criteria are considered an exact match:
1. the parameter or returning item in the invocation and the corresponding parameter or
returning item in the invoked method are the same class and category.
2. if the parameter or returning item in the invoked method is an object reference, the
corresponding parameter or returning item must follow these rules:
 If the parameter or returning item in the invoked method is a universal object
reference, the corresponding parameter or returning item in the invocation is
a universal object reference.

If the parameter or returning item in the invoked method is described with an
interface-name, the corresponding parameter or returning item in is described
with the same interface- name.

If the parameter or returning item in the invoked method is described with a
class-name, the corresponding parameter or returning item in the invocation
is described with the same class-name, and the presence or absence of the
FACTORY and ONLY phrases is the same in both descriptions.

If the parameter in the invoked method is described with the ACTIVE-CLASS
phrase and FACTORY phrase is not specified, the corresponding parameter
shall evaluate to an object reference of the same class specified in the
invocation.

If the parameter in the invoked method is described with the ACTIVE-CLASS
and FACTORY phrases, the corresponding parameter shall evaluate to an
object reference to the factory of the class specified in the invocation.
3. if the parameter or returning item in the invoked method is not an object reference, the
corresponding parameter or returning item has the same ANY LENGTH, BLANK WHEN
ZERO, JUSTIFIED, PICTURE, SIGN, and USAGE clauses, with the following additional
24 January 2005
Document Number:
J4/05-0009
Page
7 of 10
constraints:

Currency symbols match if and only if the corresponding currency strings are
the same.

Period picture symbols match if and only if the DECIMAL-POINT IS COMMA
clause is in effect for both or for neither of these interfaces. Comma picture
symbols match if and only if the DECIMAL-POINT IS COMMA clause is in
effect for both or for neither of these interfaces.
Locale specifications in the PICTURE clauses match if and only if:

both specify the same SIZE phrase in the LOCALE phrase of the PICTURE
clause, and

both specify the LOCALE phrase without a locale-name or both specify the
LOCALE phrase with the same external identification, where the external
identification is the external-locale-name or literal value associated with the
locale-name in the LOCALE clause of the SPECIAL-NAMES paragraph.
For the purpose of determining an exact match, a literal is considered to be equivalent to a data
item with no PICTURE clause and a VALUE clause that specifies that literal. If the literal is a
numeric literal the implied usage is DISPLAY; the implied picture clause consists of the same
number of the picture symbol "9" as there are digits in the literal; and the presence or absence
of signs and decimal points is the same, and the decimal point is in the same position within the
picture string as in the literal.
All parameters passed by reference are exact matches. A method invocation is considered to
match exactly if all parameters match exactly.
If two or more methods in a class match, preference is given to the method that has the greatest
number of parameters that match exactly. If the number of parameters that match exactly are
equal, preference is given first to the methods with the least number of exact matches due to
omitted optional parameters, second to those methods where the returning item is an exact
match, and then to the first method defined in the class or interface.
“
4. Page 160, 9.3.7.1.2, Conformance between interfaces:
1) Replace first paragraph and rule 1 with:
”If an interface interface-1 and an interface interface-2 are the same interface, they
conform to each other. If interface-1 and interface-2 are different interfaces, interface-1
conforms to interface-2 if and only if the entry conventions for interface-1 and interface2 are the same and for every method in interface-2 there is a method in interface-1 with
the same name that satisfies the following conditions:
1) The number of parameters are the same, with consistent BY REFERENCE and BY
VALUE specifications.”
2) Rules 2,3,4,6 and 9, first sentence of each; change in part from “of a given method” to
“of the method”.
24 January 2005
Document Number:
J4/05-0009
Page
8 of 10
3) Rule 4, delete the works “in the corresponding methods” from the end of the rule.
4) Delete rule 10.
5. Page 163, 9.3.8, Polymorphism, delete entire section.
6. Page 176, 11.6, METHOD-ID paragraph, syntax rule 3 replace in part “… with the same method
name as the method …” with “… with the same method resolution signature as the method …”
7. Page 167, 11.6, METHOD-ID paragraph, syntax rule 4a and 4b replace in part “… shall have
the same name as the method …” with “… shall have the same method resolution signature as
the method …”
8. Page 746, D.15.4.4, Interfaces and polymorphism
1) change section to D.15.4.4.1 Class polymorphism
2) Insert new D.15.4.4, Polymorphism prior to renamed section as follows:
” D.15.4.4 Polymorphism
Polymorphism is supported by COBOL in three different ways. Class polymorphism is
supported through class inheritance and the use of interfaces. Ad-hoc polymorphism is
partially supported through use of the redefines clause and class condition tests.
Parametric polymorphism is supported through method overloading.”
3) Change first paragraph, replacing in two places “polymorphism” with “class
polymorphism”.
4) Insert new sections after renamed section D.15.4.4.1 Class polymorphism.
” D.15.4.4.2 Ad-Hoc polymorphism
Through the use of the REDEFINES clause a programmer can pass different classes of
data to a method at runtime. As long as the data passed to the method is the same
length, the determination of what data is pass can be determined through class
condition tests in the method.
Example:
Method-id. AddIt.
Linkage section.
01 In1.
02 In1-X pic XX.
02 In1-9 redefines In1-X pic 99.
01 In2.
02 In2-X pic XX.
02 In2-9 redefines In2-X pic 99.
01 Out.
02 Out-X pic X(4).
02 Out-9 redefines Out-X pic 9(4).
Procedure division using In1, In2 returning Out.
If In1-9 is numeric and In2-9 is numeric
Compute Out-9 = In1-9 + In2-9
Else
24 January 2005
Document Number:
J4/05-0009
Page
9 of 10
String In1-X, In2-X delimited by size into Out-X
End if
End Method
Invoking AddIt with the input parameters of ‘AB’ and ‘CD’ would produce the result
‘ABCD’, whereas invoking AddIt with the input parameters of ‘01’ and ‘02’ would
produce ‘0003’.
Unfortunately, object references and other strongly typed items cannot be redefined.
This limits COBOL’s support of ad-hoc polymorphism to items of a class and category
that can exist within a weakly typed structure.
D.15.4.4.3 Parametric Polymorphism
Method overloading provides the ability to declare two or more methods of the same
name, but with a different number of parameters and types. Each method has a
method resolution signature that consists of the method name and all of the relevant
information from the definition of each of the parameters, and from the returning item.
During the resolution of the method, the signature derived from the invocation is
compared with all methods of the same name. If the signatures match exactly, that
method is bound. If not, the method that most closely matches the signature while still
conforming is bound.
No two methods within a class may have the same signature.
Example:
Suppose we have three methods that print. The first method invokes the print method
of whatever object pointer is passed to it. The second formats a packed decimal field
as prints it, the third prints a variable length character string.
Method-id. PrintIt.
Linkage section.
01 In-o usage object.
Procedure division using In-o.
invoke in-o ”PrintMe”.
End Method PrintIt.
Method-id. PrintIt.
Working-Storage section.
01 Out-p pic $$$,$$$.99.
Linkage section.
01 In-p pic s9(5)v99 packed-decimal.
Procedure division using In-p.
move In-p to Out-p.
display out-p.
End Method PrintIt.
Method-id. PrintIt.
Linkage section.
01 In-x pic x(20).
01 In-len pic 9(4).
Procedure division using by value In-x, In-len.
display in-x(1:in-len).
End Method PrintIt.
24 January 2005
Document Number:
J4/05-0009
Page
10 of 10
If we invoke PrintIt with an object reference as in
Invoke aClass “PrintIt” using anObject.
we would actually invoke the first method, which would invoke the “PrintMe” method of
anObject. If, on the other hand we invoked PrintIt as follows:
Invoke aClass “PrintIt” using “FooBar”, 3.
we would invoke the last method, and display “Foo”.
”
9. Page 779, E.2 Substantive changes not affecting existing programs add
”nn) Method Overloading. Within a class, methods with the same name will no longer
generate an error if the number of parameters is different, or the parameters are of different
types or lengths.
Justification
In order to more easily accept objects written external to the COBOL runtime library, it was
considered necessary to allow parametric polymorphism through method overloading. It was
considered that any program that previously compiled cleanly would not be affected by this
change.”
RESOLVED ISSUES:
NONE.
OPEN ISSUES:
1. At this point, the method resolution algorithm provides for a simple ternary test for parameters,
either an exact match, a match based on being able to be the receiving field in a move statement, or
doesn’t match at all. The algorithm then defaults to the first method that has the highest “score”. In
some cases this will result in not choosing the best fit. An example of this would be passing the
numeric literal 9. This would match both an alphanumeric field (pic x) and a numeric field (pic 9) .
Ideally we would want to resolve to the method that accepted the numeric field, however, if the
alphanumeric field came first, we would resolve to that method. We could put in an intermediate
match that is not quite exact, but matched on class and category. However this could complicate
the rules quite a bit.
Download