Type checking and method resolution


As outlined above, type checking in NetRexx is generally more relaxed than in Java (String and Rexx classes may be freely inter-assigned, for example), following the principle that assignments that do not lose information from data should normally be allowed. Permitted assignments are detailed later in this section.

For some applications, a discipline of strict type checking may be an advantage, and for these, NetRexx provides the STRICTASSIGN option. This strict checking is stronger than in Java, in that types much match exactly.

The type checking (normal or strict) is also carried over into method resolution (determining which method in a class best fits a given method invocation). In summary, this works as follows:

  1. The class of the object on which the method is being invoked is determined.
  2. Candidate methods in the class are selected. They must have the the same name and the same number of arguments as the method invocation, and it must be possible to assign the result of each argument expression to the corresponding argument in the method description (if strict assignment checking is in effect, the types must match exactly).
  3. If there are no candidate methods then the superclass of the current class is chosen and step 2 is repeated. If there is no superclass (that is, all superclasses have been tried), an error is reported.
  4. If there is just one candidate method, that method is used; the search is complete. Note that in this case superclasses of the current class are not searched for methods, even though a lower-cost method may exist at a higher level.
  5. If there is more than one candidate method, the 'cost' of the assignments from expressions to arguments is computed for each method (see below). If one has a lower cost than all others, that method is used and the search is complete. If there are two or more with the same minimum cost, and 'ambiguous method invocation' error is reported.

Cost of a method invocation

The cost of a method invocation is the sum of the costs of the assignments from each invocation argument expression to the corresponding method arguments. The cost of an assignment is 0 if the types match exactly, 1 if the assignment can be made without invoking a conversion method, but the types do not match exactly (for example assigning an 'int' to a 'long'), and 2 if an assignment would require the invocation of a conversion method (for example, assigning an 'int' to a 'Rexx' string).

More generally, NetRexx 'costs' conversions of all types, where applicable, and may use those costings in other conversion situations. Further tuning of the costing algorithms can be expected.

Permitted assignments (automatic conversions)

Unless OPTIONS STRICTASSIGN is in effect, the following assignments are permitted (this list may be expanded in the future):

  String         -> Rexx, char[], or char
  char[]         -> Rexx, String, or char
  char           -> Rexx, String, or char[]
  Rexx           -> primitive, char[], String, or char
  primitive      -> Rexx, String, char[], or char
  primitive      -> primitive (if no loss of information can take place)
  Any type       -> exactly matching type
  Any type       -> superclass of type (or an interface implemented by
                    the type or a superclass)
  null           -> any non-primitive

In the above:

Some of the conversions can cause a run-time error (exception), as when a Rexx string contains a number that is too large for an int and a Rexx to int conversion is attempted.

The boolean primitive is treated as in Rexx: a boolean is a number that may only take the values 0 or 1. A boolean may therefore be assigned to any primitive type, as well as to String or Rexx.

Case of method, property, and class names

In NetRexx, the lookup of public names will be both case-preserving and caseless. If a class or method is referenced by the name 'Foo', for example, an exact-case match will first be tried at each point that a search is made. If this succeeds, the search is complete. If it is does not succeed, a caseless search in the same context is carried out, and if one item is found, then the search is complete. If more than one item matches then an 'ambiguous reference' error is reported.

At present, lookup of external names is case-sensitive; when this restriction is lifted, for applications that require exceptional robustness, OPTIONS STRICTCASE may be specified to require that all name matches are exact (case-sensitive).


[ previous section | contents | next section ]

From 'netrexx.doc', version 1.00.
Copyright(c) IBM Corporation, 1996, 1997. All rights reserved. ©