Closed apnadkarni closed 9 years ago
Actually, according to the documentation, it should include the leaf test class:
objName info heritage Returns the current class name and the entire list of base classes in the order that they are traversed for member lookup and object destruction.
And on my system your code is working without modifications. Can we have run into an Itcl bug?
Quite possible. What version of Itcl do you have? I'm using 4.0.2
I'm using the old Itcl 3. I checked here:
https://github.com/tcltk/itcl/blob/master/doc/class.n
and the description of the info heritage
command has not changed, so I assume this is a bug in the version 4 series, but we will need a work-around, I'm afraid.
OK, found what the issue is with Itcl 4
package require Itcl
itcl::class B {
public method hierarchy {} { return [$this info heritage] }
}
itcl::class C {
inherit B
public method hierarchy {} { return [$this info heritage] }
}
itcl::class D {
inherit B
}
Then,
(tcl) 49 % C #auto
c0
(tcl) 51 % c0 hierarchy
::C ::B
(tcl) 52 % D #auto
d0
(tcl) 53 % d0 hierarchy
::B
So clearly, info heritage seems to find its class type based on where it's called from. I don't know if this is a bug or a feature. I have logged a bug against Itcl. Not hopeful of a quick fix as itcl support is rather sporadic these days.
Also found that calling from the global scope works. For example [d0 info hierarchy] in the above example works correctly. I'm going to try the workaround of using [uplevel #0 $this info heritage] instead of [$this info heritage].
Without uplevel
, can you try what happens, if you first fully qualify $this
using itcl::code
and/or namespace which
?
If we do uplevel #0
I think we will have to always use namespace which
, as well, for it to work namespace-agnostic. Not sure, I'm still learning when it comes to these nuances in Tcl.
namespace which
does not work (same effect as existing code). I got some other errors using itcl::code, most likely because I'm not really familiar with ITcl.
I landed up with [uplevel #0 [list $this] info inherit]
Do you want me to make that change and a pull request? Or make the change yourself? Or just leave it in my fork for now?
You came up with the solution, please make a merge request : ) Why is the [list $this]
necessary? And you meant to use info heritage
, I assume?
Yes, info heritage, not info inherit.
The [list] is in case the object name contains spaces, it needs to be protected as single "token" for uplevel. I don't know if Itcl permits spaces in names or not. TclOO does.
I'll make a pull request
/Ashok
By the way [namespace which] is not needed because the generated name for $this is fully qualified.
Thanks a lot for figuring this out. Closing.
I have a simple test -
Running the above, setup_before does not get called. Looking at the code for TestObj::setup and TestObj::teardown, I'm wondering at the following line:
The above loop would not include the leaf test class itself, would it? Changing the line to
fixed the problem. However, I'm not very familiar with Itcl either so perhaps I'm declaring my methods wrong?
/Ashok