View · Search · Index
No registered users in community xowiki
in last 10 minutes

Re: [Xotcl] Changing a parameter's -setter function

From: Gustaf Neumann <neumann_at_wu-wien.ac.at>
Date: Wed, 15 Mar 2006 10:46:58 +0100

Scott Gargash schrieb:
>
> Hello,
>
> Is it possible to change the -setter command associated with a
> parameter at some point other than class definition?
>
one can use the method "parameter" at any time to add/redefine
parameters (see below).
>
> I want to trigger some side effects from a parameter set operation,
> but I can't trigger the side effects until after other things have
> been initialized, so I want to use the default setter method until the
> end of init, and then replace the setter method (but not the getter
> method).
>
technically speaking, if one would like to use a single name for a
setter and getter (such as
set r [a1 foo]; a1 foo 123) there has to be a single method named "foo",
handling both cases.
this is not more than a conveniance, one can define an kind of methods
that behave as accessor
functions.
>
>
> I can override create a parameter-named method at init time, but that
> overrides the getter along with the setter. I'd rather not have to
> handle the -getter from my proc (the default -getter is fine and 2.5x
> faster). I've looked through the docs and the tutorial and I can get
> tantalizingly close (parametercmd), but is there an easy way from
> within a proc to change the -setter properties of a parameter?
>
the difference in speed is due to the fact that the default
setter/getter method is implemented in C,
whereas the tailored functions are in tcl. it is certainly possible to
program a tailored getter method
in C as well, but i doubt that this is worth the effort.

see below for more variations about your example. Aside from these options,
you can use variable traces to trigger side effects from set/get/unset
operatons.

-gustaf neumann

########################################

Class create A -parameter {{foo 1}}
A instproc foosetter {var val} {
  # Do something with [self] that isn't valid pre-init
  puts [self proc]
  my set $var $val
}

A create a1 -foo 234 ;# calls default foo setter
# redefine setter for class A
A parameter {{foo -setter foosetter -default 1}}

a1 foo ;# calls default foo getter
a1 foo 123 ;# calls overridden foosetter
puts foo=[a1 foo]

A create a2
a2 proc foo {args} {
  puts "object specific getter/setter for [self proc] $args"
    switch [llength $args] {
      0 {my set [self proc]}
      1 {my set [self proc] [lindex $args 0]}
      default {error "use [self proc] with 1 or 0 arguments"}
    }
}
a2 foo 567 ;# call proc foo as setter
puts foo=[a2 foo] ;# call proc foo as getter