option(n)

πŸ’– option(n) β€” the itcl::option glow-up guide πŸ’–

okay bestie, so this is itcl::option, a command from the [incr Tcl] world. its whole job? defining options for an extendedclass, a widget, or a widgetadaptor. that's it, that's the gig. πŸ‘‘

also, full transparency: parts of this description are "borrowed" from the Tcl extension [snit], because the functionality is mostly identical. we're standing on shoulders, no shame. 🫢


🚨 WARNING! 🚨

This is new functionality in [incr Tcl] where the API can still change!!

like... it works, but the API is still in its situationship era. do not build your whole personality on it yet. 😭


✨ SYNOPSIS ✨

option optionSpec ?defaultValue?
option optionSpec ?options?

🌸 DESCRIPTION 🌸

so the option command is what you use inside an [incr Tcl] extendedclass/widget/widgetadaptor definition to define options. it's the little declaration that says "hey, this thing has a knob, here's what it's called."

first form, first form, first form πŸ“’ β€” it defines an option for instances of this type, and optionally hands it an initial value. if you don't specify a defaultValue, the initial value defaults to the empty string. not "nothing", not null, the empty string. "". iconic.

an option defined this way is said to be locally defined. and the optionSpec? it's a list defining the option's name, resource name, and class name, like so:

option {-font font Font} {Courier 12}

now, the rules, and bestie these are rules:

  • the option name must begin with a hyphen -, and must not contain any upper case letters. lowercase only, we don't do shouting in option names.
  • the resource name and class name are optional. if you don't specify them, the resource name defaults to the option name minus the hyphen, and the class name defaults to the resource name with the first letter capitalized.

so yeah β€” this statement is equivalent to the previous example:

option -font {Courier 12}

same thing, less typing, we love efficiency. πŸ’…

for more tea about resource and class names, go see The Tk Option Database.


options are normally set and retrieved using the standard instance methods configure and cget. and inside instance code (method bodies, etc.), option values are available through the options array:

set myfont $itcl_options(-font)

look at that. chef's kiss. πŸ˜šπŸ‘Œ

second form β€” this is where you can define option handlers (like, say, -configuremethod). and ngl, if you're doing that, it should probably use configure and cget to access its options, to avoid subtle errors. subtle errors are the villain of this whole story, avoid them.


the option statement may include the following options:

  • -default defvalue β€” defines the option's default value. and if you don't? the option's default value will be "". otherwise. yeah.
  • -readonly β€” the option is handled read-only. it can only be set using configure at creation time, i.e., in the type's constructor. after that? hands off, babe. πŸ”’
  • -cgetmethod methodName β€” every locally-defined option may define a -cgetmethod; it gets called when the option's value is retrieved using the cget method. whatever the method's body returns will be the return value of the call to cget.

the named method must take one argument: the option name. for example, this code is equivalent to (though slower than) Itcl's default handling of cget:

option -font -cgetmethod GetOption
method GetOption {option} {
    return $itcl_options($option)
}

note that it's possible for any number of options to share a -cgetmethod. sharing is caring, fr fr.

  • -cgetmethodvar varName β€” that is very similar to -cgetmethod, the only difference is, one can define a variable, where to find the cgetmethod during runtime. runtime discovery, very main character. πŸ”
  • -configuremethod methodName β€” every locally-defined option may define a -configuremethod; it is called when the option's value is set using the configure or configurelist methods. and here's the important bit: it is the named method's responsibility to save the option's value. in other words, the value will not be saved to the itcl_options() array unless the method saves it there. no cap. you gotta do it yourself.

the named method must take two arguments: the option name and its new value. for example, this code is equivalent to (though slower than) Itcl's default handling of configure:

option -font -configuremethod SetOption
method SetOption {option value} {
    set itcl_options($option) $value
}

note that it's possible for any number of options to share a single -configuremethod.

  • -configuremethodvar varName β€” that is very similar to -configuremethod, the only difference is, one can define a variable, where to find the configuremethod during runtime.
  • -validatemethod methodName β€” every locally-defined option may define a -validatemethod; it is called when the option's value is set using the configure or configurelist methods, just before the -configuremethod (if any). it is the named method's responsibility to validate the option's new value, and to throw an error if the value is invalid. gatekeeping, but make it functional. 🚧

the named method must take two arguments: the option name and its new value. for example, this code verifies that -flag's value is a valid Boolean value:

option -font -validatemethod CheckBoolean
method CheckBoolean {option value} {
    if {![string is boolean -strict $value]} {
        error "option $option must have a boolean value."
    }
}

note that it's possible for any number of options to share a single -validatemethod.

  • -validatemethodvar varName β€” that is very similar to -validatemethod, the only difference is, one can define a variable, where to find the validatemethod during runtime.

πŸ”‘ KEYWORDS πŸ”‘

option, widget, widgetadaptor, extendedclass


okay that's the whole page, you ate it, you understood it, you're literally a widget whisperer now. this gem lives in the itcl manual, version 4.0, under the name option(n). go forth and define some options, bestie πŸ’–βœ¨πŸŽ€πŸ¦‹