home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / ObjectTcl-1.1 / docs / LangRef.txt < prev    next >
Encoding:
INI File  |  1995-06-30  |  16.1 KB  |  412 lines

  1. [Image]
  2.     ------------------------------------------------------------------------
  3.  
  4.  
  5. Contents
  6.  
  7.      Classes
  8.           Class Interface
  9.           Class Implementation
  10.           Inheritance
  11.           Attributes
  12.                This
  13.                Naming
  14.                Access
  15.                Arrays
  16.           Methods
  17.                Naming
  18.                Unknown
  19.                Dynamic Binding
  20.                Public & Private
  21.                Constructors
  22.                Destructors
  23.                Invocation
  24.                Results
  25.      Objects
  26.           Constructing
  27.           Destructing
  28.  
  29.     ------------------------------------------------------------------------
  30.  
  31.  
  32. Classes
  33.  
  34. In Object Tcl, a class describes the behavior and state information common to a
  35. particular collection of objects. Objects are of a particular class in the same
  36. way as variables are of a particular type.
  37.  
  38. Objects are created by instantiating a class and specifying its initial
  39. creation parameters.
  40.  
  41. There are two parts to the description of a class. The first part is the class
  42. interface which is a description of the class with respect to the outside
  43. world. The second part is the class implementation which is the description of
  44. how the behavior of the class is implemented.
  45.  
  46. Class Interface
  47.  
  48. The command:
  49.  
  50. otclInterface name ?-isA classlist? body
  51.  
  52. provides a new class interface description.
  53.  
  54. The name parameter specifies the name of the new class.
  55.  
  56. The -isA classList parameter is optional and may be used to specify class
  57. inheritance. The classList must be a list of names of classes that have already
  58. been fully described to the Object Tcl interpreter, or built-in C++ classes. No
  59. name may be repeated in the list of classes.
  60.  
  61. The body parameter is a Tcl script that describes the interface of the new
  62. class. Within the body script, the following commands may be used:
  63.  
  64. constructor argList
  65.      This command describes the interface of the constructor method for the
  66.      class in question. The argList parameter specifies a list of arguments for
  67.      the constructor. Default values may be specified.
  68.  
  69. method name argList
  70.      This command describes the interface of an instance method for the class
  71.      in question. The argList parameter specifies a list of arguments for the
  72.      instance method. Default values may be specified.
  73.  
  74. classMethod name argList
  75.      This command describes the interface of a class method for the class in
  76.      question. The argList parameter specifies a list of argments for the class
  77.      method. Default values may be specified.
  78.  
  79. All methods described in the class interface section, class or instance, are
  80. public.
  81.  
  82. Class Implementation
  83.  
  84. The command:
  85.  
  86. otclImplementation name body
  87.  
  88. provides the class implementation description.
  89.  
  90. The name parameter specifies the name of the class that this command is
  91. providing the implementation for. The class named must be a class that has
  92. previously had its class interface specified using the otclInterface command
  93. and has not had its class implementation specified.
  94.  
  95. The body parameter is a Tcl script that describes the implementation of the
  96. class. Within the body script, the following commands may be used:
  97.  
  98. constructor argList parentList body
  99.      This command describes the implementation of the constructor method. The
  100.      argList parameter specifies the arguments to the constructor, the
  101.      parentList parameter describes the construction of the parent classes and
  102.      the body parameter is a Tcl script that describes the body of the
  103.      constructor.
  104.  
  105. destructor body
  106.      This command describes the implementation of the destructor method. The
  107.      body parameter is a Tcl script that describes the body of the destructor.
  108.  
  109. method name argList body
  110.      This command describes the implementation of an instance method. The name
  111.      parameter specifies the name of the method, the argList specifies the
  112.      arguments accepted by the method and the body is a Tcl script that
  113.      describes the body of the method.
  114.  
  115. classMethod name argList body
  116.      This command describes the implementation of a class method. The name
  117.      parameter specifies the name of the method, the argList specifies the
  118.      arguments accepted by the method and the body is a Tcl script that
  119.      describes the body of the method.
  120.  
  121. attribute name ?value?
  122.      This command describes an instance attribute. The name parameter specifies
  123.      the name of the attribute and the optional value parameter specifies the
  124.      value to initialize the attribute with when an instance is created.
  125.  
  126. classAttribute name value
  127.      This command describes a class attribute. The name parameter specifies the
  128.      name of the attribute and the value parameter specifies the value to
  129.      initialize the attribute with when the otclImplementation command has
  130.      completed.
  131.  
  132. Inheritance
  133.  
  134. An Object Tcl class may use the -isA classList option in the otclInterface
  135. command to specify class inheritance. Object Tcl supports single and multiple
  136. inheritance. Object Tcl classes may inherit from other Object Tcl classes or
  137. built-in C++ classes.
  138.  
  139. Object Tcl classes may only inherit from other Object Tcl classes that have had
  140. both their interface and implementation successfully described.
  141.  
  142. Multiple inheritance of a single root class results in tree inheritance rather
  143. than lattice. Object Tcl does not provide an equivalent to the C++ virtual base
  144. classes.
  145.  
  146. Attributes
  147.  
  148. Attributes are used to record state information within classes and objects.
  149. There are two types of attribute: instance and class. Instance attributes have
  150. separate instantiations for each object of that class. Each object may alter
  151. its instance attributes independently of any other object of that class. Class
  152. attributes have only one instance that is shared by all objects of that class.
  153.  
  154. Both instance and class attributes are private in that they are not accessible
  155. from outside the class, only from within the class by class or instance method
  156. bodies.
  157.  
  158. Both instance attributes and class attributes may have an initial value
  159. specified. For class attributes, the initial value is mandatory. Instance
  160. attributes are initialized with their initial value or "" before the
  161. constructor for that class is executed. Class attributes are initialized with
  162. their initial value when the class implementation description is complete.
  163.  
  164. This
  165.  
  166. An implicit instance attribute exists called this. This attribute is only in
  167. scope within an instance method and its value is a reference to the current
  168. object. The this attribute may be used to invoke other instance methods upon
  169. the current object or be passed to other commands as an argument.
  170.  
  171. It is forbidden to modify the value of this, but this is not detected by the
  172. Object Tcl system.
  173.  
  174. Naming
  175.  
  176. Class attributes and instance attributes share the same name space and must be
  177. unique within a class.
  178.  
  179. The name "this" is reserved.
  180.  
  181. Access
  182.  
  183. From within an instance method all instance attributes and class attributes are
  184. available as local Tcl variables. The Tcl $ substitution syntax may be used.
  185.  
  186. From within class methods all class attributes are available as local Tcl
  187. variables. The Tcl $ substitution syntax may be used.
  188.  
  189. Arrays
  190.  
  191. Object Tcl supports instance and class array attributes. The description of an
  192. array attribute takes the following form:
  193.  
  194. attribute name() ?value?
  195.      The brackets after the attribute name, with no white space, indicates that
  196.      the attribute is an array. If an initial value is supplied it must be a
  197.      list of {key value} pairs. For example:
  198.  
  199.      attribute sales() {{jan 12} {feb 13} {mar 44}}
  200.  
  201. Array attributes may be manipulated in the same way as Tcl arrays using the $
  202. substitution syntax with a key as a bracketed suffix. For example:
  203.  
  204. puts $sales(mar)
  205.  
  206. Methods
  207.  
  208. Methods are used to give behavior to a class of object. There are two types of
  209. methods: class and instance. Class methods operate at the class scope and may
  210. manipulate class attributes. Instance methods operate on an object scope and
  211. must be invoked upon an object of that class. Instance methods may access class
  212. attributes and instance attributes. Instance methods may invoke class methods.
  213. Class methods may only invoke instance methods if an object reference is
  214. available.
  215.  
  216. Naming
  217.  
  218. Method names must be unique within a class. Instance and class methods of the
  219. same class share the same name space.
  220.  
  221. If a class uses inheritance then it may describe a method with the same name as
  222. a method in one of its parent classes. This is allowed as the dynamic binding
  223. resolves the name at invocation time.
  224.  
  225. A class may also describe a class method with the same name as a class or
  226. instance method in one of its parent classes. In this case the syntax of class
  227. method invocation identifies the class to find the method in.
  228.  
  229. The names "constructor" and "destructor" are reserved.
  230.  
  231. Naming an instance method unknown implies specific behavior.
  232.  
  233. Unknown
  234.  
  235. Object Tcl classes, and even C++ classes exported to Object Tcl, may have an
  236. instance method called unknown. The unknown method is invoked automatically by
  237. the Object Tcl system when an attempt is made to invoke a method upon the
  238. object that is not in the class's implementation.
  239.  
  240. The unknown method is called with the arguments specified in the original
  241. method invocation with the name of the requested method prepended.
  242.  
  243. The unknown method may be inherited from a superclass and the rules of dynamic
  244. binding operate.
  245.  
  246. Dynamic Binding
  247.  
  248. Classes that use inheritance may provide methods that are also provided by
  249. their parent classes. Dynamic binding is the mechanism whereby the method of
  250. the most specific class is executed. This allows classes to specialize the
  251. classes they inherit from to make them more specific.
  252.  
  253. Given an object of a particular class, the invocation of a method involves a
  254. search through the inheritance hierarchy for that method. The search starts at
  255. the most specific class, the instantiated class, and proceeds in a depth-first
  256. search. If multiple inheritance is used, the list of superclasses is searched
  257. in the order they were given in the -isA classList option of the otclInterface
  258. command. There is no checking for two descriptions of the named method at the
  259. same level in the hierarchy. The first method in the search order will be
  260. executed.
  261.  
  262. Public & Private Methods
  263.  
  264. Class or instance methods may be public or private. If a method is public then
  265. it may be invoked by any other piece of Tcl code. If a method is private then
  266. it may only be invoked from another method of that class.
  267.  
  268. Methods are public if their interface is described in the otclInterface
  269. command, otherwise they are private.
  270.  
  271. For public methods, default parameter values must be specified in the class
  272. interface. Private methods must have their default values specified in the
  273. class implementation.
  274.  
  275. Constructors
  276.  
  277. The constructor method is a special instance method that is invoked
  278. automatically upon the construction of a new instance of the class.
  279. Constructors may be used to initialize attribute values and perform
  280. initialization behavior.
  281.  
  282. The implementation description for a constructor method contains a parameter
  283. called parentList. This list contains a description of the parameters to pass
  284. to the constructor of any inherited classes. The parentList takes the form of a
  285. list of scripts, where each script starts with the name of the parent class and
  286. is followed by a series of Tcl expressions that are to be passed to the parent
  287. class constructor. If an inherited class constructor takes no arguments then it
  288. need not appear as an item in the parentList. The expressions in the parentList
  289. may make use of any class or instance attributes of the calling class or any of
  290. the formal arguments passed to the calling class constructor.
  291.  
  292. The parentList parameter is mandatory even for classes that do not use
  293. inheritance. In this case they must specify an empty parentList.
  294.  
  295. Constructors are optional.
  296.  
  297. When using inheritance, the most general class constructor body is evaluated
  298. first and the most specific last. If multiple inheritance is used then the
  299. constructor for the inherited classes are executed in the order they are
  300. described in the -isA classList option of the otclInterface command for that
  301. class.
  302.  
  303. A constructor is considered an instance method in that the this attribute is
  304. available and may be used to invoke other methods upon the object. The dynamic
  305. binding of methods will operate correctly but it must be remembered that the
  306. object is undergoing construction. Using dynamic binding it would be possible
  307. to invoke a method from a part of the class hierarchy for that object that is
  308. still waiting for its constructor to be evaluated.
  309.  
  310. Destructors
  311.  
  312. The destructor is a special method that cannot be public (there is no command
  313. appropriate for making it public anyway). Destructors are the opposite to
  314. constructors in that they are invoked automatically upon the destruction of an
  315. instance of the class.
  316.  
  317. Destructors are optional.
  318.  
  319. Destructors are executed in the reverse order to constructors. The most
  320. specific destructor is executed first, and if multiple inheritance is used the
  321. destructors for the multiple superclasses are invoked in the reverse order to
  322. that specified in the -isA classList option of the otclInterface command for
  323. that class.
  324.  
  325. As with constructors, destructors are treated the same as instance methods.
  326. They may call other methods upon the object, so it is possible to call methods
  327. upon parts of the object that have already had their destructor invoked.
  328.  
  329. Invocation
  330.  
  331. Class methods and instance methods are invoked in different ways.
  332.  
  333. A class method may be invoked using the following syntax:
  334.  
  335. class method ?args ...?
  336.  
  337. where class is the name of the class to invoke the method upon, method is the
  338. name of the class method to invoke and arg is an argument to pass to the class
  339. method.
  340.  
  341. An instance method may be invoked using the following syntax:
  342.  
  343. obref method ?arg ...?
  344.  
  345. where obref is an object reference, method is the name of the method to invoke
  346. and arg is an argument to pass to the instance method.
  347.  
  348. Another instance method may be invoked from within the body of another instance
  349. method using the following syntax:
  350.  
  351. $this ?-parent? method ?arg ...?
  352.  
  353. where method is the name of the method to invoke and arg is an argument to pass
  354. to the instance method. The ?-parent? option may be used to force the
  355. invocation of the method from the immediate parent class. This facility may be
  356. used to override the dynamic binding feature, thus allowing the most specific
  357. version of a method to utilize a more general version of the method. The parent
  358. class must be one of the classes named in the -isA classList section of the
  359. otclInterface command.
  360.  
  361. Constructors and destructors cannot be invoked explicitly.
  362.  
  363. Results
  364.  
  365. Class methods and instance methods may return results using the Tcl return
  366. command as in normal Tcl procedures.
  367.     ------------------------------------------------------------------------
  368.  
  369.  
  370. Objects
  371.  
  372. Objects in Object Tcl are instances of classes. All instances of the same class
  373. share common behavior, through methods, and maintain similar state information,
  374. through attributes.
  375.  
  376. Objects are constructed by instantiating an Object Tcl class or a built-in C++
  377. class. Methods may then be invoked upon that object which alter its state.
  378. Finally an object may be destroyed.
  379.  
  380. Constructing
  381.  
  382. An object is constructed by instantiating a class using the following command:
  383.  
  384. otclNew class ?arg...?
  385.  
  386. where class specifies the name of the class to instantiate and arg specifies an
  387. argument to pass to the constructor of the class. The result of this command is
  388. a reference to the new object. This reference may be used to invoke methods
  389. upon that object.
  390.  
  391. Destructing
  392.  
  393. When an object is finished with it may be deleted using the following command:
  394.  
  395. otclDelete objref
  396.  
  397. where objref is a reference to an Object Tcl object as created by the otclNew
  398. command.
  399.  
  400. After an object has been deleted, all references to that object are invalid and
  401. must not be used. The result of dereferencing an invalid reference will vary
  402. depending on the object mapping implementation. If the object mapper is capable
  403. of detecting the dereference then a Tcl error will be reported, otherwise the
  404. dereference will result in memory misuse and probable core dump.
  405.     ------------------------------------------------------------------------
  406.  
  407. Object Tcl | Overview | Language Reference | C++ Binding Reference | Example |
  408. Source Code
  409.     ------------------------------------------------------------------------
  410.  
  411. otcl@x.co.uk
  412.