Provided by: tcl9.0-doc_9.0.1+dfsg-1_all bug

NAME

       my, myclass - invoke any method of current object or its class

SYNOPSIS

       package require tcl::oo

       my methodName ?arg ...?
       myclass methodName ?arg ...?
________________________________________________________________________________________________________________

DESCRIPTION

       The  my  command  is used to allow methods of objects to invoke methods of the object (or its class), and 2
       the myclass command is used to allow methods of objects to invoke methods of the  current  class  of  the 2
       object  as  an  object.   In particular, the set of valid values for methodName is the set of all methods
       supported by an object and its superclasses, including those that are not exported and private methods of 2
       the object or class when used within another method defined by that object or class.

       The object upon which the method is invoked via my is the one that owns the namespace that the my command
       is contained in initially (NB: the link remains if the  command  is  renamed),  which  is  the  currently
       invoked  object  by  default.   Similarly,  the  object on which the method is invoked via myclass is the 2
       object that is the current class of the object that owns  the  namespace  that  the  myclass  command  is 2
       contained  in  initially.  As  with  my,  the  link  remains  even if the command is renamed into another 2
       namespace, and defaults to being the manufacturing class of the current object.

       Each object has its own my and myclass commands, contained in its instance namespace.

EXAMPLES

       This example shows basic use of my to use the variables method of the  oo::object  class,  which  is  not
       publicly visible by default:

              oo::class create c {
                  method count {} {
                      my variable counter
                      puts [incr counter]
                  }
              }

              c create o
              o count               prints "1"
              o count               prints "2"
              o count               prints "3"

       This  example shows how you can use my to make callbacks to private methods from outside the object (from
       a trace), using namespace code  to  enter  the  correct  context.  (See  the  callback  command  for  the
       recommended way of doing this.)

              oo::class create HasCallback {
                  method makeCallback {} {
                      return [namespace code {
                          my Callback
                      }]
                  }

                  method Callback {args} {
                      puts "callback: $args"
                  }
              }

              set o [HasCallback new]
              trace add variable xyz write [$o makeCallback]
              set xyz "called"      prints "callback: xyz {} write"

       This  example  shows  how  to access a private method of a class from an instance of that class. (See the 2
       classmethod declaration in oo::define for a higher level interface for doing this.)                       2

              oo::class create CountedSteps {                                                                    2
                  self {                                                                                         2
                      variable count                                                                             2
                      method Count {} {                                                                          2
                          return [incr count]                                                                    2
                      }                                                                                          2
                  }                                                                                              2
                  method advanceTwice {} {                                                                       2
                      puts "in [self] step A: [myclass Count]"                                                   2
                      puts "in [self] step B: [myclass Count]"                                                   2
                  }                                                                                              2
              }                                                                                                  2

              CountedSteps create x                                                                              2
              CountedSteps create y                                                                              2
              x advanceTwice        prints "in ::x step A: 1"                                                   2
                                    prints "in ::x step B: 2"                                                   2
              y advanceTwice        prints "in ::y step A: 3"                                                   2
                                    prints "in ::y step B: 4"                                                   2
              x advanceTwice        prints "in ::x step A: 5"                                                   2
                                    prints "in ::x step B: 6"                                                   2
              y advanceTwice        prints "in ::y step A: 7"                                                   2
                                    prints "in ::y step B: 8"                                                   2

SEE ALSO

       next(3tcl), oo::object(3tcl), self(3tcl)

KEYWORDS

       method, method visibility, object, private method, public method

TclOO                                                  0.1                                              my(3tcl)