home *** CD-ROM | disk | FTP | other *** search
- NetRexx 1.122
- NetRexx language quick start
- ============================
-
- Copyright(c) IBM Corporation, 1996, 1997. All rights reserved.
-
- Introduction
- """"""""""""
- NetRexx is a new programming language derived from both Rexx and
- Java(tm); it is a dialect of Rexx that retains the portability and
- efficiency of Java, while being as easy to learn and to use as Rexx.
-
- NetRexx is an effective alternative to the Java language. With NetRexx,
- you can create programs and applets for the Java environment more easily
- than by programming in Java. Using Java classes is especially easy in
- NetRexx as you rarely have to worry about all the different types of
- numbers and strings that Java requires.
-
- This document summarises the main features of NetRexx, and is intended
- to help you start using it quickly. It's assumed that you have some
- knowledge of programming in a language such as Rexx, C, BASIC, or Java,
- but a knowledge of 'object-oriented' programming isn't needed.
-
- This is not a complete tutorial, though -- think of it more as a
- 'taster'; it covers the main points of the language and shows some
- examples you can try or modify.
-
- For more samples (and examples of using Java classes from NetRexx), and
- for more formal details of the language, please see the other NetRexx
- documents at http://www2.hursley.ibm.com/netrexx/ -- you'll find the
- NetRexx software to download there, too.
-
- NetRexx programs
- """"""""""""""""
- The structure of a NetRexx program is extremely simple. This sample
- program, 'toast', is complete, documented, and executable as it stands:
-
- /* This wishes you the best of health. */
- say 'Cheers!'
-
- This program consists of two lines: the first is an optional comment
- that describes the purpose of the program, and the second is a SAY
- statement. SAY simply displays the result of the expression following
- it -- in this case just a literal string (you can use either single or
- double quotes around strings, as you prefer).
-
- To run this program, edit a file called toast.nrx and copy or paste the
- two lines above into it. You can then use the NetRexxC Java program to
- compile it, and the java command to run it:
-
- java COM.ibm.netrexx.process.NetRexxC toast
- java toast
-
- You may also be able to use the NETREXXC command to compile and run the
- program with a single command (details may vary -- see the installation
- and user's guide document):
-
- netrexxc toast -run
-
- Of course, NetRexx can do more than just display a character string.
- Although the language has a simple syntax, and has a small number of
- statement types, it is powerful; the language allows full access to the
- rapidly growing collection of Java programs known as 'class libraries',
- and allows new class libraries to be written in NetRexx.
-
- The rest of this document introduces most of the features of NetRexx.
- Since the economy, power, and clarity of expression in NetRexx is best
- appreciated with use, you are urged to try using the language yourself.
-
- Expressions and variables
- """""""""""""""""""""""""
- Like SAY in the 'toast' example, many statements in NetRexx include
- _expressions_ that will be evaluated. NetRexx provides arithmetic
- operators (including integer division, remainder, and power operators),
- several concatenation operators, comparison operators, and logical
- operators. These can be used in any combination within a NetRexx
- expression (provided, of course, that the data values are valid for
- those operations).
-
- All the operators act upon strings of characters (known as _Rexx
- strings_), which may be of any length (typically limited only by the
- amount of storage available). Quotes (either single or double) are used
- to indicate literal strings, and are optional if the literal string is
- just a number. For example, the expressions:
-
- '2' + '3'
- '2' + 3
- 2 + 3
-
- would all result in '5'.
-
- The results of expressions are often assigned to _variables_, using a
- conventional assignment syntax:
-
- var1=5 /* sets var1 to '5' */
- var2=(var1+2)*10 /* sets var2 to '70' */
-
- You can write the names of variables (and keywords) in whatever mixture
- of uppercase and lowercase that you prefer; the language is not
- case-sensitive.
-
- This next sample program, 'greet', shows expressions used in various
- ways:
-
- /* A short program to greet you. */
- /* First display a prompt: */
- say 'Please type your name and then press ENTER:'
- answer=ask /* Get the reply into ANSWER */
-
- /* If no name was entered, then use a fixed greeting, */
- /* otherwise echo the name politely. */
- if answer='' then say 'Hello Stranger!'
- else say 'Hello' answer'!'
-
- After displaying a prompt, the program reads a line of text from the
- user ('ask' is a keyword provided by NetRexx) and assigns it to the
- variable ANSWER. This is then tested to see if any characters were
- entered, and different actions are taken accordingly; if the user typed
- 'Fred' in response to the prompt, then the program would display:
-
- Hello Fred!
-
- As you see, the expression on the last SAY (display) statement
- concatenated the string 'Hello' to the value of variable ANSWER with a
- blank in between them (the blank is here a valid operator, meaning
- 'concatenate with blank'). The string '!' is then directly concatenated
- to the result built up so far. These unobtrusive operators (the _blank
- operator_ and abuttal) for concatenation are very natural and easy to
- use, and make building text strings simple and clear.
-
- The layout of statements is very flexible. In the 'greet' example, for
- instance, the IF statement could be laid out in a number of ways,
- according to personal preference. Line breaks can be added at either
- side of the THEN (or following the ELSE).
-
- In general, statements are ended by the end of a line. To continue a
- statement to a following line, you can use a hyphen (minus sign) just as
- in English:
-
- say 'Here we have an expression that is quite long, so' -
- 'it is split over two lines'
-
- This acts as though the two lines were all on one line, with the hyphen
- and any blanks around it being replaced by a single blank. The net
- result is two strings concatenated together (with a blank in between)
- and then displayed.
-
- When desired, multiple statements can be placed on one line with the
- aid of the semicolon separator:
-
- if answer='Yes' then do; say 'OK!'; exit; end
-
- (many people find multiple statements on one line hard to read, but
- sometimes it is convenient).
-
- Control statements
- """"""""""""""""""
- NetRexx provides a selection of _control_ statements, whose form was
- chosen for readability and similarity to natural languages. The control
- statements include IF... THEN... ELSE (as in the 'greet' example) for
- simple conditional processing:
-
- if ask='Yes' then say "You answered Yes"
- else say "You didn't answer Yes"
-
- SELECT... WHEN... OTHERWISE... END for selecting from a number of
- alternatives:
-
- select
- when a>0 then say 'greater than zero'
- when a<0 then say 'less than zero'
- otherwise say 'zero'
- end
-
- DO... END for grouping:
-
- if a>3 then do
- say 'A is greater than 3; it will be set to zero'
- a=0
- end
-
- and LOOP... END for repetition:
-
- loop i=1 to 10 /* repeat 10 times; I changes from 1 to 10 */
- say i
- end
-
- The LOOP statement can be used to step a variable TO some limit, BY some
- increment, FOR a specified number of iterations, and WHILE or UNTIL some
- condition is satisfied. LOOP FOREVER is also provided. Loop execution
- may be modified by LEAVE and ITERATE statements that significantly
- reduce the complexity of many programs.
-
- NetRexx arithmetic
- """"""""""""""""""
- Character strings in NetRexx are commonly used for arithmetic (assuming,
- of course, that they represent numbers). The string representation of
- numbers can include integers, decimal notation, and exponential
- notation; they are all treated the same way. Here are a few:
-
- '1234'
- '12.03'
- '-12'
- '120e+7'
-
- The arithmetic operations in NetRexx are designed for people rather than
- machines, so are decimal rather than binary, do not overflow at certain
- values, and follow the rules that people use for arithmetic. The
- operations are completely defined by the ANSI standard for Rexx, so
- correct implementations will always give the same results.
-
- An unusual feature of NetRexx arithmetic is the NUMERIC statement: this
- may be used to select the _arbitrary precision_ of calculations. You
- may calculate to whatever precision that you wish, for financial
- calculations, perhaps, limited only by available memory. For example:
-
- numeric digits 50
- say 1/7
-
- which would display
-
- 0.14285714285714285714285714285714285714285714285714
-
- The numeric precision can be set for an entire program, or be adjusted
- at will within the program. The NUMERIC statement can also be used to
- select the notation (_scientific_ or _engineering_) used for numbers in
- exponential format.
-
- NetRexx also provides simple access to the native binary arithmetic of
- computers. Using binary arithmetic offers many opportunities for
- errors, but is useful when performance is paramount. You select binary
- arithmetic by adding the statement:
-
- options binary
-
- at the top of a NetRexx program. The language processor will then use
- binary arithmetic instead of Rexx decimal arithmetic for calculations,
- throughout the program.
-
- Doing things with strings
- """""""""""""""""""""""""
- Another thing Rexx is good for is manipulating strings in various ways.
- NetRexx provides the same facilities as Rexx, but with a syntax that is
- more like Java or other similar languages:
-
- phrase='Now is the time for a party'
- say phrase.word(7).pos('r')
-
- The second line here can be read from left to right as "take the
- variable 'phrase', find the seventh word, and then find the position
- of the first 'r' in that word". This would display '3' in this case,
- because 'r' is the third character in 'party'.
-
- (In Rexx, the second line above would have been written using nested
- function calls:
-
- say pos('r', word(phrase, 7))
-
- which is not as easy to read; you have to follow the nesting and then
- backtrack from right to left to work out exactly what's going on.)
-
- In the NetRexx syntax, at each point in the sequence of operations some
- routine is acting on the result of what has gone before. These routines
- are called _methods_, to make the distinction from functions (which act
- in isolation). NetRexx provides (as methods) most of the functions that
- were evolved for Rexx, for example:
-
- o changestr (change all occurrences of a substring to another)
- o copies (make multiple copies of a string)
- o lastpos (find rightmost occurrence)
- o left and right (return leftmost/rightmost character(s))
- o reverse (swap end-to-end)
- o space (pad between words with fixed spacing)
- o strip (remove leading and/or trailing white space)
- o pos and wordpos (find the position of string or a word in a string)
- o verify (check the contents of a string for selected characters)
- o word, wordindex, wordlength, and words (work with words)
-
- These and the others like them, and the parsing described in the next
- section, make it especially easy to process text with NetRexx.
-
- Parsing strings
- """""""""""""""
- The previous section described some of the string-handling facilities
- available; NetRexx also provides Rexx string parsing, which is a fast
- and simple way of breaking up strings of characters using simple pattern
- matching.
-
- A PARSE statement first specifies the string to be parsed, often taken
- simply from a variable. This is followed by a _template_ which
- describes how the string is to be split up, and where the pieces are to
- be put.
-
- --- Parsing into words ---
-
- The simplest form of parsing template consists of a list of variable
- names. The string being parsed is split up into words (sequences of
- characters separated by blanks), and each word from the string is
- assigned (copied) to the next variable in turn, from left to right. The
- final variable is treated specially in that it will be assigned a copy
- of whatever is left of the original string and may therefore contain
- several words. For example, in:
-
- parse 'This is a sentence.' v1 v2 v3
-
- the variable v1 would be assigned the value 'This', v2 would be assigned
- the value 'is', and v3 would be assigned the value 'a sentence.'.
-
- --- Literal patterns ---
-
- A literal string may be used in a template as a pattern to split up the
- string. For example
-
- parse 'To be, or not to be?' w1 ',' w2 w3 w4
-
- would cause the string to be scanned for the comma, and then split at
- that point; each section is then treated in just the same way as the
- whole string was in the previous example.
-
- Thus, w1 would be set to 'To be', w2 and w3 would be assigned the values
- 'or' and 'not', and w4 would be assigned the remainder: 'to be?'. Note
- that the pattern itself is not assigned to any variable.
-
- The pattern may be specified as a variable, by putting the variable name
- in parentheses. The following statements:
-
- comma=','
- parse 'To be, or not to be?' w1 (comma) w2 w3 w4
-
- therefore have the same effect as the previous example.
-
- --- Positional patterns ---
-
- The third kind of parsing mechanism is the numeric positional pattern.
- This works just like the string pattern except in syntax; it specifies a
- column number (which may be absolute or relative, and derived from a
- variable if necessary).
-
- String patterns and positional patterns can be mixed.
-
- Indexed variables
- """""""""""""""""
- NetRexx provides an indexed variable mechanism, adapted from the
- compound variables of Rexx.
-
- NetRexx string variables can be referred to simply by name, or also by
- their name qualified by another string (the _index_). When an index is
- used, a value associated with that index is either set or extracted; in
- the latter case, the initial value of the variable is returned if the
- index has not been used to set a value. For example, the program:
-
- dognoise='bark'
- dognoise['pup']='yap'
- dognoise['bulldog']='grrrrr'
- say dognoise['pup'] dognoise['terrier'] dognoise['bulldog']
-
- would display
-
- yap bark grrrrr
-
- Any expression may be used inside the brackets; the resulting string is
- used as the index. Multiple dimensions may be used, if required:
-
- dognoise='bark'
- dognoise['spaniel', 'brown']='ruff'
- say dognoise['spaniel', 'brown'] dognoise['terrier']
-
- which would display
-
- ruff bark
-
- Here's a more complex example, a test program with a function (called
- a _constant method_ in NetRexx) that removes all duplicate words from a
- string of words:
-
- /* justonetest.nrx -- test the justone function. */
- say justone('to be or not to be') /* simple testcase */
- exit
-
- /* This removes duplicate words from a string, and */
- /* shows the use of a variable (HADWORD) which is */
- /* indexed by arbitrary data (words). */
- method justone(wordlist) constant
- hadword=0 /* show all possible words as new */
- outlist='' /* initialize the output list */
- loop while wordlist\='' /* loop while we have data */
- /* next, split WORDLIST into first word and residue */
- parse wordlist word wordlist
- if hadword[word] then iterate /* loop if had word */
- hadword[word]=1 /* remember we have had this word */
- outlist=outlist word /* add word to output list */
- end
- return outlist /* finally return the result */
-
- Running this program would display just the four words 'to', 'be', 'or',
- and 'not'.
-
- This example also uses the built-in _string parsing_ provided by the
- PARSE statement. In this instance, the value of WORDLIST is parsed,
- with the first word of the value being assigned to the variable WORD and
- the remainder being assigned back to WORDLIST (replacing the original
- value).
-
- [Author's note: since the notation for indexed variables looks just like
- arrays (see the next section), but does not not suffer the restrictions
- of arrays, I like to call them _disarrays_.]
-
- Arrays
- """"""
- NetRexx also supports Java's fixed-size _arrays_. These are an ordered
- set of items, indexed by integers. To use an array, you first have to
- construct it; an individual item may then be selected by an index whose
- value must be in the range 0 through N-1, where N is the number of items
- in the array:
-
- array=String[3] -- make an array of three Java Strings
- array[0]='String one' -- set each array item
- array[1]='Another string'
- array[2]='foobar'
- loop i=0 to 2 -- display them
- say array[i]
- end
-
- This example also shows NetRexx _line comments_; the sequence '--'
- (outside of literal strings or '/*' comments) indicates that the
- remainder of the line is not part of the program and is commentary.
-
- Tracing
- """""""
- Defined as part of the language, NetRexx tracing often provides useful
- debugging information. The flow of execution of programs may be traced,
- and the execution trace can be viewed as it occurs or captured in a
- file. The trace can show each clause as it is executed, and optionally
- show the results of expressions, _etc._ For example, the program:
-
- trace results
- number=1/7
- parse number before '.' after
- say after'.'before
-
- would result in the trace:
-
- 2 *=* number=1/7
- >v> number "0.142857143"
- 3 *=* parse number before '.' after
- >v> before "0"
- >v> after "142857143"
- 4 *=* say after'.'before
- >>> "142857143.0"
-
- where the lines marked with '*=*' are the statements in the program,
- lines with '>v>' show results assigned to local variables, and
- lines with '>>>' show results of un-named expressions.
-
- Exception and error handling
- """"""""""""""""""""""""""""
- NetRexx doesn't have a GOTO statement, but a SIGNAL statement is
- provided for abnormal transfer of control, such as when something
- unusual occurs. Using SIGNAL raises an _exception_; all control
- statements are then 'unwound' until the exception is caught by a control
- statement that specifies a suitable CATCH statement for handling the
- exception.
-
- Exceptions are also raised when various errors occur, such as attempting
- to divide a number by zero. For example:
-
- say 'Please enter a number:'
- number=ask
- do
- say 'The reciprocal of' number 'is:' 1/number
- catch Exception
- say 'Sorry, could not divide "'number'" into 1'
- end
-
- Here, the CATCH statement will catch any exception that is raised when
- the division is attempted (conversion error, divide by zero, etc.).
-
- Any control statement that ends with END (DO, LOOP, or SELECT) may be
- modified with one or more CATCH statements to handle exceptions.
-
- Things that aren't strings
- """"""""""""""""""""""""""
- In all the examples so far, the data being manipulated (numbers, words,
- and so on) are expressed as a string of characters. Many things,
- however, can be expressed more easily in some other way, so NetRexx
- allows variables to refer to other collections of data, which are known
- as _objects_.
-
- Objects are defined by a name that lets NetRexx determine the data
- and methods that are associated with the object. This name identifies
- the type of the object, and is usually called the _class_ of the object.
-
- For example, an object of class Oblong might represent an oblong to be
- manipulated and displayed. The oblong could be defined by two values:
- its width and its height. These values are called the _properties_ of
- the Oblong class.
-
- Most methods associated with an object perform operations on the object;
- for example a 'size' method might be provided to change the size of an
- Oblong object. Other methods are used to construct objects (just as
- for NetRexx arrays, an object must be constructed before it can be
- used). In NetRexx and Java, these _constructor_ methods always have the
- same name as the class of object that they build (Oblong, in this case).
-
- Here's how an Oblong class might be written in NetRexx (by convention,
- this would be written in a file called Oblong.nrx; Java expects the name
- of the file to match the name of the class inside it):
-
- /* Oblong.nrx -- simple oblong class */
- class Oblong
- width -- size (X dimension)
- height -- size (Y dimension)
-
- /* Constructor method to make a new oblong */
- method Oblong(new_width, new_height)
- -- when we get here, a new (uninitialized) object has been
- -- created. Copy the parameters we have been given to the
- -- properties of the object:
- width=new_width; height=new_height
-
- /* Change the size of an Oblong */
- method size(new_width, new_height) returns Oblong
- width=new_width; height=new_height
- return this -- return the resized object
-
- /* Change the size of an Oblong, relative to its current size */
- method sizerelative(rel_width, rel_height) returns Oblong
- width=width+rel_width; height=height+rel_height
- return this
-
- /* 'Print' what we know about the oblong */
- method print
- say 'Oblong' width 'x' height
-
- To summarize:
-
- 1. A class is started by the 'class' statement, which names the
- class.
-
- 2. The class statement is followed by a list of the properties of the
- object. These can be assigned initial values, if required.
-
- 3. The properties are followed by the methods of the object. Each
- method is introduced by a method statement which names the method
- and describes the arguments that must be supplied to the method.
- The body of the method is ended by the next method statement (or
- by the end of the file).
-
- The Oblong.nrx file is compiled just like any other NetRexx program, and
- should create a _class file_ called Oblong.class. Here's a program to
- try out the Oblong class:
-
- /* tryOblong.nrx -- try the Oblong class */
-
- first=Oblong(5,3) -- make an oblong
- first.print -- show it
- first.sizerelative(1,1).print -- enlarge it and print it again
-
- second=Oblong(1,2) -- make another oblong
- second.print -- and print it
-
- when 'tryOblong.nrx' is compiled, you'll notice that the cross-reference
- listing of variables shows that the variables 'first' and 'second' have
- type 'Oblong'. These variables refer to Oblongs, just as the variables
- in earlier examples referred to Rexx strings.
-
- Once a variable has been assigned a type, it can only refer to objects
- of that type. This helps avoid errors where a variable refers to an
- object that it wasn't meant to.
-
- --- Programs are classes, too ---
-
- It's worth pointing out, here, that all the example programs in this
- document are in fact classes (you may have noticed that compiling them
- creates xxx.class files, where xxx is the name of the source file). The
- Java environment will allow a class to run as a stand-alone
- _application_ if it has a constant method called 'main' which takes an
- array of Java Strings as its argument.
-
- If necessary (that is, if there is no class statement) NetRexx
- automatically adds the necessary class and method statement, and also
- a statement to convert the array of strings (each of which holds one
- word from the command string) to a single Rexx string. The 'toast'
- example could therefore have been written:
-
- /* This wishes you the best of health. */
- class toast
- method main(argwords=String[]) constant; arg=Rexx(argwords)
- say 'Cheers!'
-
-
- Extending classes
- """""""""""""""""
- It's common, when dealing with objects, to take an existing class and
- extend it. One way to do this is to modify the source code of the
- original class -- but this isn't always available, and with many
- different people modifying a class, classes could rapidly get
- over-complicated.
-
- Languages that deal with objects, like NetRexx, therefore allow new
- classes of objects to be set up which are derived from existing classes.
- For example, if you wanted a different kind of Oblong in which the
- Oblong had a new property that would be used when printing the Oblong as
- a rectangle, you might define it thus:
-
- /* charOblong.nrx -- an oblong class with character */
- class charOblong extends Oblong
- printchar -- the character for display
-
- /* Constructor method to make a new oblong with character */
- method charOblong(new_width, new_height, new_printchar)
- super(new_width, new_height) -- make an oblong
- printchar=new_printchar -- and set the print character
-
- /* 'Print' the oblong */
- method print
- loop for super.height
- say printchar.copies(super.width)
- end
-
- There are several things worth noting about this example:
-
- 1. The 'extends Oblong' on the class statement means that this class
- is an extension of the Oblong class. The properties and methods of
- the Oblong class are _inherited_ by this class (that is, appear as
- though they were part of this class).
-
- Another common way of saying this is that 'charOblong' is a
- _subclass_ of 'Oblong' (and 'Oblong' is the _superclass_ of
- 'charOblong').
-
- 2. This class adds the 'printchar' property to the properties already
- defined for Oblong.
-
- 3. The constructor for this class takes a width and height (just like
- Oblong) and adds a third argument to specify a print character. It
- first invokes the constructor of its superclass (Oblong) to build
- an Oblong, and finally sets the printchar for the new object.
-
- 4. The new charOblong object also prints differently, as a rectangle
- of characters, according to its dimension. The 'print' method (as
- it has the same name and arguments -- none -- as that of the
- superclass) replaces (overrides) the 'print' method of Oblong.
-
- 5. The other methods of Oblong are not overridden, and therefore can
- be used on charOblong objects.
-
- The charOblong.nrx file is compiled just like Oblong.nrx was, and
- should create a file called charOblong.class. Here's a program to
- try it out:
-
- /* trycharOblong.nrx -- try the charOblong class */
-
- first=charOblong(5,3,'#') -- make an oblong
- first.print -- show it
- first.sizerelative(1,1).print -- enlarge it and print it again
-
- second=charOblong(1,2,'*') -- make another oblong
- second.print -- and print it
-
- This should create the two charOblong objects, and print them out in a
- simple 'character graphics' form. Note the use of the method
- 'sizerelative' from Oblong to resize the charOblong object.
-
- --- Optional arguments ---
-
- All methods in NetRexx may have optional arguments (omitted from the
- right) if desired. For an argument to be optional, you must supply a
- default value. For example, if the charOblong constructor was to have a
- default printchar value, its method statement could have been written:
-
- method charOblong(new_width, new_height, new_printchar='X')
-
- which indicates that if no third argument is supplied then 'X' should be
- used. A program creating a charOblong could then simply write:
-
- first=charOblong(5,3) -- make an oblong
-
- which would have the same effect as if 'X' were specified as the third
- argument.
-
- Binary types and conversions
- """"""""""""""""""""""""""""
- The Java environment supports, and indeed requires, the notion of
- fixed-precision 'primitive' binary types, which correspond closely to
- the binary operations usually available at the hardware level in
- computers. In brief, these types are:
-
- o _byte, short, int,_ and _long_ -- signed integers that will fit in
- 8, 16, 32, or 64 bits respectively
-
- o _float_ and _double_ -- signed floating point numbers that will fit
- in 32 or or 64 bits respectively.
-
- o _char_ -- an unsigned 16-bit quantity, holding a Unicode character
-
- o _boolean_ -- a 1-bit logical value, representing 'false' or 'true'.
-
- Objects of these types are handled specially by the environment '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 Java (String, char, char[], and Rexx) and
- the primitive types listed above. 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 at compile time that the
- conversion is safe (as in int -> String) or it will be detected at run
- time if the conversion fails (as in String -> int).
-
- The automatic conversions greatly simplify the writing of programs for
- the Java environment: 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, 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 Java, and ensures that all types are
- an exact match.
-
- 2. options binary -- uses Java fixed precision arithmetic on binary
- types (also, literal numbers, for example, will be treated as
- binary, and local variables will be given 'native' Java types such
- as _int_ or _String_, where possible).
-
- Binary arithmetic currently gives better performance than Rexx
- decimal arithmetic, but places the burden of avoiding overflows and
- loss of information on the programmer.
-
- The options statement (which may list more than one option) is placed
- before the first class statement in a file.
-
- You may also explicitly assign a type to an expression or variable:
-
- i=int 3000000 -- 'i' is an 'int' with initial value 3000000
- j=int 4000000 -- 'j' is an 'int' with initial value 4000000
- k=int -- 'k' is assigned type 'int', with no initial value
- say i*j -- carry out multiplication and display the result
- k=i*j -- carry out multiplication and assign result to 'k'
-
- This example also illustrates one difference between 'options nobinary'
- and 'options binary'. With the former (the NetRexx default) the SAY
- would display '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 the window
- maxx=size.width-1
- maxy=size.height-1
- loop y=0 to maxy
- col=Color.getHSBColor(y/maxy, 1, 1) -- select a colour
- g.setColor(col) -- set it
- g.drawLine(0, y, maxx, y) -- and fill a 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
- particular example, 'options binary' may be added to the top of the
- program with no other changes being necessary.
-
-
- Summary and Information Sources
- """""""""""""""""""""""""""""""
- The NetRexx language, as you will have seen, allows the writing of
- programs for the Java environment with a minimum of overhead and
- 'boilerplate syntax'; using NetRexx for writing Java classes could
- increase your productivity by 30% or more.
-
- Further, by simplifying the variety of numeric and string types of Java
- down to a single class that follows the rules of Rexx strings,
- programming is greatly simplified. Where necessary, however, full
- access to all Java types and classes is available.
-
- Other examples are available, including both stand-alone applications
- and samples of applets for Java-enabled browsers (for example, an applet
- that plays an audio clip, and another that displays the time in
- English). You can find these from the NetRexx web pages, at
-
- http://www2.hursley.ibm.com/netrexx/
-
- Also at that location, you'll find a more in-depth treatment of the
- language, and downloadable packages containing the NetRexx software and
- documentation. The software should run on any platform that supports
- the Java Development Kit.
-