version 1.42, 2025/05/05 04:46:35
|
version 1.44, 2025/05/05 05:16:34
|
Line 4
|
Line 4
|
@settitle The FreeM Manual |
@settitle The FreeM Manual |
|
|
@copying |
@copying |
This manual is for FreeM, (version 0.64.0-rc1), which is a free software@footnote{FreeM subscribes to the software licensing philosophy described in @emph{Free Software, Free Society: |
This manual is for FreeM, (version 0.64.0-rc1), which is a free software implementation of the M programming language. |
Selected Essays of Richard M. Stallman}.} implementation of the M programming language. |
|
|
|
Print-optimized versions of this book are typeset in @emph{Computer Modern} by the author using the @emph{GNU Texinfo} tools. |
Print-optimized versions of this book are typeset in @emph{Computer Modern} by the author using the @emph{GNU Texinfo} tools. |
|
|
Line 4590 SET MYSTRING="This is a ""string literal
|
Line 4589 SET MYSTRING="This is a ""string literal
|
@cindex types, custom |
@cindex types, custom |
@cindex classes |
@cindex classes |
|
|
See @ref{Classes}. |
See @ref{Object-Oriented Programming}. |
|
|
@node Globals |
@node Globals |
@chapter Globals |
@chapter Globals |
Line 4945 If you use other M implementations, you
|
Line 4944 If you use other M implementations, you
|
@cindex object-oriented programming |
@cindex object-oriented programming |
@cindex programming, object-oriented |
@cindex programming, object-oriented |
|
|
@menu |
|
* Classes:: The basis of object-oriented programming. |
|
* Inheritance:: Basing one class upon another. |
|
* Methods:: Attaching code to a class. |
|
* Public and Private Variables:: Managing class member access. |
|
* Instantiating Objects:: Creating instances of classes. |
|
* Determining Object Class:: Getting object information at runtime. |
|
@end menu |
|
|
|
@node Classes |
|
@section Classes |
@section Classes |
|
|
|
|
@node Class Overview |
|
@subsection Class Overview |
@subsection Class Overview |
|
|
A @emph{class} is the primary organizing concept of FreeM support for object-oriented programming, and in FreeM, is simply an M routine with a few special properties: |
A @emph{class} is the primary organizing concept of FreeM support for object-oriented programming, and in FreeM, is simply an M routine with a few special properties: |
Line 4976 DESTROY(THIS) ; This is the destructor
|
Line 4963 DESTROY(THIS) ; This is the destructor
|
|
|
The above example demonstrates general class syntax. |
The above example demonstrates general class syntax. |
|
|
@node Constructors |
|
@subsection Constructors |
@subsection Constructors |
|
|
A @emph{constructor} is an M entry point that is called when a new instance of a class is created. |
A @emph{constructor} is an M entry point that is called when a new instance of a class is created. |
Line 5001 A constructor looks like this:
|
Line 4987 A constructor looks like this:
|
|
|
In the above example, @emph{<superclass>} represents the name of a class from which this class should inherit. In this case, the @code{FRACTION} class inherits from the @code{OBJECT} class. Note that this is not strictly necessary in this case, as all classes in FreeM automatically inherit from @code{OBJECT}. |
In the above example, @emph{<superclass>} represents the name of a class from which this class should inherit. In this case, the @code{FRACTION} class inherits from the @code{OBJECT} class. Note that this is not strictly necessary in this case, as all classes in FreeM automatically inherit from @code{OBJECT}. |
|
|
@node Destructors |
|
@subsection Destructors |
@subsection Destructors |
A @code{destructor} is called when you @code{KILL} an instance variable. Its tag must be @code{DESTROY}, and it must take one argument (@code{THIS}). |
A @code{destructor} is called when you @code{KILL} an instance variable. Its tag must be @code{DESTROY}, and it must take one argument (@code{THIS}). |
|
|
Line 5015 DESTROY(THIS) ;
|
Line 5000 DESTROY(THIS) ;
|
Q |
Q |
@end example |
@end example |
|
|
@node Inheritance |
|
@section Inheritance |
@section Inheritance |
|
|
Every class you create will automatically inherit the methods and functionality of the @code{OBJECT} class, supplied with FreeM. |
Every class you create will automatically inherit the methods and functionality of the @code{OBJECT} class, supplied with FreeM. |
Line 5028 Inheritance is achieved by specifying th
|
Line 5012 Inheritance is achieved by specifying th
|
CLASS(THIS,INIT):SUPERCLASS |
CLASS(THIS,INIT):SUPERCLASS |
@end example |
@end example |
|
|
@node Runtime Polymorphism |
|
@subsection Runtime Polymorphism |
@subsection Runtime Polymorphism |
|
|
You can achieve runtime polymorphism by subclassing, and defining methods in the subclass that match the names of existing methods in the superclass. Following FreeM inheritance rules, the overridden method in the subclass will be called, and the method in the superclass will not. |
You can achieve runtime polymorphism by subclassing, and defining methods in the subclass that match the names of existing methods in the superclass. Following FreeM inheritance rules, the overridden method in the subclass will be called, and the method in the superclass will not. |
|
|
Note that the overridden method in the subclass can take a different set or number of arguments than the @emph{formallist} of the superclass method would specify. |
Note that the overridden method in the subclass can take a different set or number of arguments than the @emph{formallist} of the superclass method would specify. |
|
|
@node Methods |
|
@section Methods |
@section Methods |
Class methods are defined as tags with @emph{formallist}s in a class routine, and per the typical FreeM object pattern, must take at least one argument, being @code{THIS} (representing a reference to the object instance being accessed). |
Class methods are defined as tags with @emph{formallist}s in a class routine, and per the typical FreeM object pattern, must take at least one argument, being @code{THIS} (representing a reference to the object instance being accessed). |
|
|
Line 5058 DEFAULT.USER> W MYOBJ.MYMETHOD()
|
Line 5040 DEFAULT.USER> W MYOBJ.MYMETHOD()
|
VALUE |
VALUE |
@end example |
@end example |
|
|
@node Public and Private Variables |
|
@section Public and Private Variables |
@section Public and Private Variables |
|
|
FreeM supports private fields with the @code{:PRIVATE} specifier in the @code{SET} command, enforcing classical object-oriented data encapsulation. The @code{:PUBLIC} specifier is provided for completeness, and is the default. |
FreeM supports private fields with the @code{:PRIVATE} specifier in the @code{SET} command, enforcing classical object-oriented data encapsulation. The @code{:PUBLIC} specifier is provided for completeness, and is the default. |
Line 5081 Either of the following commands will cr
|
Line 5062 Either of the following commands will cr
|
|
|
Attempting to access private fields from outside of the class will raise error condition @code{ZOBJFLDACCV}. |
Attempting to access private fields from outside of the class will raise error condition @code{ZOBJFLDACCV}. |
|
|
@node Instantiating Objects |
|
@section Instantiating Objects |
@section Instantiating Objects |
|
|
To instantiate an object (i.e., create an object from a certain class), you will use the @code{NEW} command as follows: |
To instantiate an object (i.e., create an object from a certain class), you will use the @code{NEW} command as follows: |
Line 5092 NEW MYSTR=$#^%STRING("myString")
|
Line 5072 NEW MYSTR=$#^%STRING("myString")
|
|
|
This will create a local variable called MYSTR of type STRING, and initialize it with the value myString. |
This will create a local variable called MYSTR of type STRING, and initialize it with the value myString. |
|
|
@node Determining Object Class |
|
@section Determining Object Class |
@section Determining Object Class |
|
|
To determine the class of any FreeM local variable, you will use the @code{$$TYPE()} method: |
To determine the class of any FreeM local variable, you will use the @code{$$TYPE()} method: |