mvanle
mvanle

Reputation: 2015

How to use itcl::delegation in TCL?

The following delegation does not work.

How do I get it to work ?

package require itcl

itcl::extendedclass Tail {
    method wag {} {
        return "Wag, wag, wag"
    }
}

itcl::extendedclass Dog {
    delegate method wag to tail

    constructor {} {
        set tail [Tail #auto]
    }
}

puts [info patchlevel]
puts $itcl::patchLevel

Dog dog
dog wag

Screenshot of error:

enter image description here

Upvotes: 1

Views: 104

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137767

After poking around, I find that the problem is that the Tail instance has been created in the Dog namespace (instead of in the instance namespace for dog or in the global scope). That's weird, but couples to the fact that the current namespace in the constructor is the ::Dog namespace. That wouldn't be a problem except Itcl instance construction returns unqualified names and the current namespace when the delegate is processed isn't the same as when the Tail instance is made.

My evidence? This:

% info class instances Tail
::Dog::tail0

But it does suggest that we can fix this by changing the definition of Dog to be:

itcl::extendedclass Dog {
    delegate method wag to tail

    constructor {} {
        # Convert the name into its fully qualified form right now
        set tail [namespace which [Tail #auto]]
    }
}

With that, we can do:

% Dog dog
dog
% dog wag
Wag, wag, wag

Looks OK to me.

Upvotes: 1

Related Questions