configbody(3itcl)

πŸ’… configbody β€” the itcl one, bestie

okay so this is configbody(3itcl), and it lives in the [incr Tcl] manual, and I need you to lock in because it's actually so good once it clicks 🫢


✨ NAME ✨

itcl::configbody β€” change the "config" code for a public variable

like. that's it. that's the whole vibe. you're swapping out the lil code block that runs when somebody configures a public variable πŸ’–


🌸 SYNOPSIS 🌸

itcl::configbody className::varName body

πŸ¦‹ DESCRIPTION πŸ¦‹

so the configbody command is what you use outside of an [incr Tcl] class definition to define or redefine the configuration code that's associated with a public variable. and why do we care? because public variables act like configuration options for an object πŸŽ€ they can be modified outside the class scope using the built-in configure method, which is so slay of them.

and each variable can have a lil bit of "config" code associated with it that gets automatically executed when the variable is configured. configbody is the command that lets you define or redefine that body of code. iconic behaviour, no notes ⭐

lowkey, just like the body command, this whole facility lets a class definition have separate "interface" and "implementation" parts:

  • the "interface" part is a class command with declarations for methods, procs, instance variables and common variables ✨
  • the "implementation" part is a series of body and configbody commands πŸ’…

and if you keep the "implementation" part in a separate file, you can source it again and again as bugs get fixed, to support interactive development. which is the whole point, honestly.

when you're using the "tcl" mode in the emacs editor, the "interface" and "implementation" parts can live in the same file; as bugs are fixed, individual bodies can be highlighted and sent to the test application. chef's kiss 🧚

the name className::varName identifies the public variable being updated. now here's the tea:

  • if the body string starts with "@", it's treated as the symbolic name for a C procedure πŸ‘€
  • otherwise, it's treated as a Tcl command script 🐚

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.

in the following example, the procedure My_FooObjCmd() is registered with the symbolic name "foo". this procedure can be referenced in the configbody 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 πŸ“

bestie here's the main character moment. in the following example, a "File" class is defined to represent open files. whenever the -name option is configured, the existing file is closed, and a new file is opened. πŸ”₯

and note this: the "config" code for a public variable is optional. the -access option, for example, does not have it. don't come for me, that's just how it is πŸ’€
itcl::class File {
    private variable fid ""

    public variable name ""
    public variable access "r"

    constructor {args} {
        eval configure $args
    }
    destructor {
        if {$fid != ""} {
            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]
}

itcl::configbody File::name {
    if {$fid != ""} {
        close $fid
    }
    set fid [open $name $access]
}

#
# See the File class in action:
#
File x

x configure -name /etc/passwd
while {![x eof]} {
    puts "=> [x get]"
}
itcl::delete object x

see how the configbody File::name block is doing the actual work the moment -name gets configured? yeah. that's the whole thing. that's the girlboss move πŸ‘‘


πŸ‘‘ KEYWORDS πŸ‘‘

class, object, variable, configure


and that's the page, bestie!! πŸ’œ this one came straight outta the itcl 3.0 era of the configbody(3itcl) manpage β€” mwah, sending it off with a kiss πŸŽ€πŸ’‹

πŸ“¦ where this page comes from