Wize /
Method
Search:  

Method

The method command provides a wrapped version of proc for use with Tod to add an object argument and upvar. Wize implements method natively, but Mod implements it in Tcl as:

    proc method {name arglst body} {
        uplevel 1 [list ::proc $name "_ $arglst" "upvar \$_ {};$body"]
    }

Thus the following two definitions are identical.

  proc foo {_ a} {
     upvar $_ {}
     return $(x)$a
  }

  method foo {a} {
     return $(x)$a
  }

Be advised that although method definitions may be cleaner looking than declarations with proc, there can (in larger applications) be disadvantages to doing so. For one thing, proc code just works with existing Tcl tools such as procheck. Thus caution should be exercised when choosing to use method over proc.

© 2008 Peter MacDonald

Page last modified on June 04, 2009, at 08:24 AM