1

Using Tcl8.5, is there a difference between calling $this inside a method and not calling it? e.g.:

package require Itcl
namespace import itcl::*

class MyCls {
    method foo {}
    method bar {}
}

body MyCls::bar {} { return "hi" }

body MyCls::foo {} {
  puts [$this bar]
  # OR
  puts [bar]
}
Dor
  • 7,344
  • 4
  • 32
  • 45
  • This is not specific to tcl but normaly this is used to differ the signature (parameters) from an object field. Eg. you have a class with the field "bar" then u have a method with the parameter bar also -> with this.bar = bar u can define which bar (local or field) you want to access. – F. Müller Feb 26 '14 at 07:34

1 Answers1

0

You only have to add one global implementation of bar to try this out. As shown below - there is no difference. The class just adds another level to be checked for the procedure before it gets to the global namespace.

% package require Itcl
% namespace import itcl::*
% class C {method foo {}; method bar {}}
% body C::bar {} {return "C::bar"}
% body C::foo {} { puts [$this bar]; puts [bar] }
% C c
c
% c foo
C::bar
C::bar
%
patthoyts
  • 32,320
  • 3
  • 62
  • 93
  • Didn't understand "The class just adds ... global namespace". Indeed I can try this out, but maybe I'll be missing some difference in functionality by this simple test... – Dor Feb 26 '14 at 08:07
  • @Dor When Tcl looks up how to perform a command, it logically looks along a “path” of namespaces to see if the command is present there. By default, first it looks in the current namespace, then in the global namespace. Itcl inserts the class namespace in between those two (and does some other magic to make methods appear as commands). Tcl 8.5 allows you to insert your own extras in the path as well. – Donal Fellows Feb 26 '14 at 09:45