Poet2.2 Manual |
Prototype Object Extension for Tcl |
Methods
The syntax for invoking a method is:
<object> <method> ?arguments...?
The syntax for declaring a method is:
# Comments
#
<object> method {<arguments>} {
<body>
}
The arguments to a method follow all the same rules as for Tcl proc's
(a method is a Tcl proc, underneath). Arguments can have default
values and there can be a variable number of arguments, expressed just
as with Tcl procs. In addition, the object the method is being
invoked on is available within the method as $self
(it actually becomes the first argument to the proc). So Poet
method arguments are identical to Tcl proc arguments, with the
limitation that you can't use the name "self", since it's already being
used.
Much of the Poet library uses "_" to prefix a method not meant to be
invoked except by other methods of the same object, following the same
convention as for private slots, but this is just a naming convention.
There is no protection against invoking a private method outside the
object that created it. Both the public and private methods of an
object are saved if it is a persistent object.
When a method is invoked on an object, the inheritance tree is searched for an implementation of that method (see Inheritance). If none is found, the method DEFAULT is invoked, if it exists. If no DEFAULT method is defined, an error is returned. NOTE:
The Poet objects that are wrappers around Tk, Ttk, and BWidget widgets (any
descendant of ProtoWidget) define a DEFAULT method that invokes the
method as a command on the wrapped widget. Invoking a method that
isn't either a method on the ProtoWidget or a command known to its
wrapped widget will generate an error message from the wrapped widget.
When a method overrides an ancestor's implementation, it often needs to invoke the ancestor's version of the method. The method as
executes a method found on another object. Here's an example of an
object that overrides its construct method to add an extra argument:
Object construct PairedObject
PairedObject slot other ""
PairedObject method construct {name other} {
set kid [$self as Object construct $name]
$kid slot other $other
return $kid
}
In fact, as can be used to invoke any object's method on any other
object, the two objects do not have to be related in any way.