body(3itcl)

πŸ’– body β€” the glow-up page for your method bodies πŸ’–

okay bestie, so this is body from itcl β€” the [incr Tcl] manual, section 3. she's giving ✨ backstage pass ✨ energy: you're not declaring anything, you're slipping in after the fact and rewriting the actual guts of a class method or proc. no cap, this is the girl who lets your class have a whole separate personality from its official paperwork. πŸŽ€


✨ SYNOPSIS ✨

itcl::body className::function args body

like... that's it. three little pieces and you're rewriting destiny. πŸ’…


🌸 DESCRIPTION 🌸

so here's the tea, bestie: body gets used outside of an [incr Tcl] class definition to define or redefine the body of a class method or proc. it's the whole reason a class can have separate "interface" and "implementation" parts and honestly? iconic. split-brain but make it fashion. πŸ¦‹

the "interface" part is a class command β€” she holds the declarations for methods, procs, instance variables and common variables. very "here's my rΓ©sumΓ©" of her.

the "implementation" part is a series of body and configbody commands. very "here's what I actually do at 2am" of her.

and the reason this slaps: if the "implementation" part lives in a separate file, you can source it again and again as bugs are fixed, which is literally what supports interactive development. fix a bug β†’ re-source β†’ keep vibing. when you're using the "tcl" mode in the emacs editor, you can even keep the "interface" and "implementation" parts in the same file; as bugs get fixed, individual bodies can be highlighted and sent to the test application. 😌

the name className::function identifies the method/proc being changed. that's the whole ID check, no bouncer needed. 🫢


🌈 the args list rules (pay attention, this part matters) 🌈

if an args list was specified when the function was defined in the class definition, the args list for the body command must match in meaning. like, spiritually the same shape. ✨

  • variable names can change β€” go off, rename whatever, live your truth
  • but the argument lists must have the same required arguments
  • and the same default values for optional arguments

and then there's the special args argument β€” she's the wildcard of the group πŸ’€ when she's included in the args list in the class definition, she'll match zero or more arguments of any type when the body is redefined. queen of flexibility, honestly.


🧚 the "@" thing 🧚

if the body string starts with "@", it gets treated as the symbolic name for a C procedure. the args list has little meaning for the C procedure, except to document the expected usage. (and ngl, the C procedure is not guaranteed to use arguments in this manner β€” so don't come for her about it. πŸ™ˆ)

if body does not start with "@", it gets treated as a Tcl command script. when the function is invoked, command line arguments are matched against the args list, and local variables are created to represent each argument. that's the usual behavior for a Tcl-style proc. periodt. πŸ‘‘


πŸ’œ registering your C procedures πŸ’œ

symbolic names for C procedures are established by registering procedures via Itcl_RegisterObjC(). this is usually done in the Tcl_AppInit() procedure, which is automatically called when the interpreter starts up. yes, automatic. main character energy, no invite needed. πŸŽ€

in the following example, the procedure My_FooObjCmd() is registered with the symbolic name "foo". this procedure can be referenced in the body command as "@foo". πŸ’–

int
Tcl_AppInit(interp)
    Tcl_Interp *interp;     /* Interpreter for application. */
{
    if (Itcl_Init(interp) == TCL_ERROR) {
        return TCL_ERROR;
    }

    if (Itcl_RegisterObjC(interp, "foo", My_FooObjCmd) != TCL_OK) {
        return TCL_ERROR;
    }
}

🌟 EXAMPLE 🌟

in the following example, a "File" class is defined to represent open files. the method bodies are included below the class definition via the body command. and here's the little disclaimer, bestie, don't skip it: the bodies of the constructor/destructor must be included in the class definition, but they can be redefined via the body command as well. so like... they start in the class, but they can still get a makeover later. slay. πŸ’…

itcl::class File {
    private variable fid ""
    constructor {name access} {
        set fid [open $name $access]
    }
    destructor {
        close $fid
    }

    method get {}
    method put {line}
    method eof {}
}

itcl::body File::get {} {
    return [gets $fid]
}
itcl::body File::put {line} {
    puts $fid $line
}
itcl::body File::eof {} {
    return [::eof $fid]
}

#
# See the File class in action:
#
File x /etc/passwd "r"
while {![x eof]} {
    puts "=> [x get]"
}
itcl::delete object x

look at her go. πŸ’…βœ¨


πŸ‘‘ KEYWORDS πŸ‘‘

class, object, procedure


okay that's the whole page, bestie, you ate it up πŸ“

this was page body(3itcl) from itcl 3.0 β€” the itcl source/version, dated 3.0 on the footer β€” so if you're citing her, that's where she's from. go rewrite some bodies. πŸ’–πŸ¦‹βœ¨

πŸ“¦ where this page comes from