NetRexx overview - Binary types and conversions

NetRexx Overview, version 2.02 Copyright (c) IBM Corporation, 2001. All rights reserved. © | 22 May 2001
[previous | contents | next]

## Binary types and conversions

Most programming environments support the notion of fixed-precision ‘primitive’ binary types, which correspond closely to the binary operations usually available at the hardware level in computers. For the reference implementation, these types are:

- byte, short, int, and long – signed integers that will fit in 8, 16, 32, or 64 bits respectively
- float and double – signed floating point numbers that will fit in 32 or 64 bits respectively.
- char – an unsigned 16-bit quantity, holding a Unicode character
- boolean – a 1-bit logical value, representing `0` or `1` (‘false’ or ‘true’).

Objects of these types are handled specially by the implementation ‘under the covers’ in order to achieve maximum efficiency; in particular, they cannot be constructed like other objects – their value is held directly. This distinction rarely matters to the NetRexx programmer: in the case of string literals an object is constructed automatically; in the case of an `int` literal, an object is not constructed.

Further, NetRexx automatically allows the conversion between the various forms of character strings in implementations[1] and the primitive types. The ‘golden rule’ that is followed by NetRexx is that any automatic conversion which is applied must not lose information: either it can be determined before execution that the conversion is safe (as in `int` to `String`) or it will be detected at execution time if the conversion fails (as in `String` to `int`).

The automatic conversions greatly simplify the writing of programs; the exact type of numeric and string-like method arguments rarely needs to be a concern of the programmer.

For certain applications where early checking or performance override other considerations, the reference implementation of NetRexx provides options for different treatment of the primitive types:

1. options strictassign – ensures exact type matching for all assignments. No conversions (including those from shorter integers to longer ones) are applied. This option provides stricter type-checking than most other languages, and ensures that all types are an exact match.
2. options binary – uses implementation-dependent fixed precision arithmetic on binary types (also, literal numbers, for example, will be treated as binary, and local variables will be given ‘native’ types such as `int` or `String`, where possible).
   Binary arithmetic currently gives better performance than NetRexx decimal arithmetic, but places the burden of avoiding overflows and loss of information on the programmer.

The options instruction (which may list more than one option) is placed before the first class instruction in a file; the binary keyword may also be used on a class or method instruction, to allow an individual class or method to use binary arithmetic.

#### Explicit type assignment

You may explicitly assign a type to an expression or variable:

      i=int 3000000  -- 'i' is an 'int' with value 3000000
      j=int 4000000  -- 'j' is an 'int' with value 4000000
      k=int          -- 'k' is an 'int', with no initial value
      say i*j        -- multiply and display the result
      k=i*j          -- multiply and assign result to 'k'

This example also illustrates an important difference between options nobinary and options binary. With the former (the default) the say instruction would display the result ‘`1.20000000E+13`’ and a conversion overflow would be reported when the same expression is assigned to the variable `k`.

With options binary, binary arithmetic would be used for the multiplications, and so no error would be detected; the say would display ‘`-138625024`’ and the variable `k` takes the incorrect result.

#### Binary types in practice

In practice, explicit type assignment is only occasionally needed in NetRexx. Those conversions that are necessary for using existing classes (or those that use options binary) are generally automatic. For example, here is an ‘Applet’ for use by Java-enabled browsers:

      /* A simple graphics Applet */
      class Rainbow extends Applet
        method paint(g=Graphics)  -- called to repaint window
          maxx=size.width-1
          maxy=size.height-1
          loop y=0 to maxy
            col=Color.getHSBColor(y/maxy, 1, 1) -- new colour
            g.setColor(col)                     -- set it
            g.drawLine(0, y, maxx, y)           -- fill slice
          end y

In this example, the variable `col` will have type `Color`, and the three arguments to the method `getHSBColor` will all automatically be converted to type `float`. As no overflows are possible in this example, options binary may be added to the top of the program with no other changes being necessary.

Footnotes:

[1] | In the reference implementation, these are `String`, `char`, `char[]` (an array of characters), and the NetRexx string type, `Rexx`.

[previous | contents | next]

From The NetRexx Language by Mike Cowlishaw, mfc@uk.ibm.com (ISBN 0-13-806332-X, 197pp, Prentice-Hall, 1997).