Topic: APLX Help : Help on APL language : System Functions & Variables : ⎕CR Canonical Representation
[ Previous | Next | Contents | Index | APL Home ]

www.microapl.co.uk

⎕CR Canonical Representation


Converts a user-defined function, operator, method, or class into a character matrix or vector. The name of the item to be converted to text form is the right argument of ⎕CR. ⎕CR can be used monadically, or optionally with a left argument. The value of the left argument determines the shape of the result.

Left Argument Result
Omitted or 0 Character matrix
1 Character vector with embedded carriage-returns separating lines
2 Vector with each item a vector of characters corresponding to one line of the text form

The matrix or vector shows the lines of the function, operator, method or class, with line numbers omitted. It has the characteristics of any object composed of characters. You can, for example, use indexing to alter its elements, or use the and functions on it. And, if you want, you can arrange for another function to make the alterations for you by specifying within that function the alterations you require. To convert a function called DT to character data

             MATRIX←⎕CR 'DT'         (for a matrix result)
             VECTOR←1 ⎕CR 'DT'       (for a simple delimited vector result)
             VECTOR←2 ⎕CR 'DT'       (for a nested vector result)

See also ⎕FX for the reverse operation, which takes the text form and fixes it as a function, operator or class.

Canonical representation of a class

⎕CR can also be used to convert an entire class to text form. In this case, the right argument should be a class name. The format of the result is as follows:

  • The first line is the class header. This comprises the name of the class, followed (if the class inherits from another class) by a colon and the name of the parent class. Any private members of the class (i.e. names which are local to the class) are then listed, separated by semi-colons. The header line ends with a left curly brace '{' character.
  • The properties of the class are then listed, one per line. The name of the property is listed first. If it has a default value, an assignment arrow follows, and then the transfer form of the expression which initializes the property. If the property is read-only, two assignment arrows are used. If the property is class-wide (i.e. there is only a single copy shared between all instances in the workspace, then whole line is enclosed in curly braces.
  • The methods of the class (and the constructor, if any) are then listed, with a del character starting and ending each method.
  • Finally, the class ends with a closing right curly brace, on a line by itself.

For example:

      ⎕CR 'Point'
Point; IncSerial;serial {
Z←0
Y←0
X←0
{serial←0}
{CATEGORY←←'Geometric'}

∇IncSerial
⍝ Increment serial number
serial←serial+1
∇

∇R←Mag
⍝ Return magnitude (distance from origin)
R←+/(X,Y,Z)*2
R←R*0.5
∇

∇Point B
⍝ Constructor for Class B.  Optionally set up X Y Z
:If 0≠⍴B
  (X Y Z)←B
:EndIf
IncSerial
∇

∇R←GetSerial
R←serial
∇
}

In the above example, the class Point has three instance properties (X Y and Z), all initialized to 0. It has two class-wide properties serial and CATEGORY. CATEGORY is a read-only property, so is initialized with a double assignment arrow. The serial property is private (i.e. not accessible from outside the class), so it is localized in the class header. The class has three ordinary methods (of which one, IncSerial is private), and a constructor (identifiable by the fact that it has the same name as the class).

If a second class MovingPoint inherits from Point, and adds a new property VELOCITY, then you might have:

      ⎕CR 'MovingPoint'
MovingPoint : Point {
VELOCITY←0
}

This shows the use of the colon character in the header, to indicate that MovingPoint inherits from Point.

Canonical representation of a single method from a class

⎕CR can also be used to convert a single method from a given class to text form. In this case, the right argument should be the fully-qualified method name (i.e. the class name, a period, and the method name). For example:

      ⎕CR 'Point.Mag'
R←Mag
⍝ Return magnitude (distance from origin)
R←+/(X,Y,Z)*2
R←R*0.5

Topic: APLX Help : Help on APL language : System Functions & Variables : ⎕CR Canonical Representation
[ Previous | Next | Contents | Index | APL Home ]