1body(n) [incr Tcl] body(n)
2
3
4
5______________________________________________________________________________
6
8 itcl::body - change the body for a class method/proc
9
11 itcl::body className::function args body
12______________________________________________________________________________
13
14
16 The body command is used outside of an [incr Tcl] class definition to
17 define or redefine the body of a class method or proc. This facility
18 allows a class definition to have separate "interface" and "implementa‐
19 tion" parts. The "interface" part is a class command with declarations
20 for methods, procs, instance variables and common variables. The
21 "implementation" part is a series of body and configbody commands. If
22 the "implementation" part is kept in a separate file, it can be sourced
23 again and again as bugs are fixed, to support interactive development.
24 When using the "tcl" mode in the emacs editor, the "interface" and
25 "implementation" parts can be kept in the same file; as bugs are fixed,
26 individual bodies can be highlighted and sent to the test application.
27
28 The name "className::function" identifies the method/proc being
29 changed.
30
31 If an args list was specified when the function was defined in the
32 class definition, the args list for the body command must match in
33 meaning. Variable names can change, but the argument lists must have
34 the same required arguments and the same default values for optional
35 arguments. The special args argument acts as a wildcard when included
36 in the args list in the class definition; it will match zero or more
37 arguments of any type when the body is redefined.
38
39 If the body string starts with "@", it is treated as the symbolic name
40 for a C procedure. The args list has little meaning for the C proce‐
41 dure, except to document the expected usage. (The C procedure is not
42 guaranteed to use arguments in this manner.) If body does not start
43 with "@", it is treated as a Tcl command script. When the function is
44 invoked, command line arguments are matched against the args list, and
45 local variables are created to represent each argument. This is the
46 usual behavior for a Tcl-style proc.
47
48 Symbolic names for C procedures are established by registering proce‐
49 dures via Itcl_RegisterC(). This is usually done in the Tcl_AppInit()
50 procedure, which is automatically called when the interpreter starts
51 up. In the following example, the procedure My_FooCmd() is registered
52 with the symbolic name "foo". This procedure can be referenced in the
53 body command as "@foo".
54 int
55 Tcl_AppInit(interp)
56 Tcl_Interp *interp; /* Interpreter for application. */
57 {
58 if (Itcl_Init(interp) == TCL_ERROR) {
59 return TCL_ERROR;
60 }
61
62 if (Itcl_RegisterC(interp, "foo", My_FooCmd) != TCL_OK) {
63 return TCL_ERROR;
64 }
65 }
66
67
69 In the following example, a "File" class is defined to represent open
70 files. The method bodies are included below the class definition via
71 the body command. Note that the bodies of the constructor/destructor
72 must be included in the class definition, but they can be redefined via
73 the body command as well.
74 itcl::class File {
75 private variable fid ""
76 constructor {name access} {
77 set fid [open $name $access]
78 }
79 destructor {
80 close $fid
81 }
82
83 method get {}
84 method put {line}
85 method eof {}
86 }
87
88 itcl::body File::get {} {
89 return [gets $fid]
90 }
91 itcl::body File::put {line} {
92 puts $fid $line
93 }
94 itcl::body File::eof {} {
95 return [::eof $fid]
96 }
97
98 #
99 # See the File class in action:
100 #
101 File x /etc/passwd "r"
102 while {![x eof]} {
103 puts "=> [x get]"
104 }
105 itcl::delete object x
106
107
109 class, object, procedure
110
111
112
113itcl 3.0 body(n)