|
Binary types and conversionsMost 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:
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:
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 assignmentYou 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 practiceIn 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 yIn 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:
[previous | contents | next] From
The NetRexx Language by
Mike Cowlishaw,
|