如何在 TCL 中使用 itcl::delegation? [英] How to use itcl::delegation in TCL?

查看:30
本文介绍了如何在 TCL 中使用 itcl::delegation?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下委托无效.

我如何让它工作?

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

错误截图:

推荐答案

翻来覆去,发现问题是Tail实例已经在Dog中创建> 命名空间(而不是在 dog 的实例命名空间中或在全局范围内).这很奇怪,但与构造函数中的当前命名空间是 ::Dog 命名空间的事实相结合.这不会成为问题,除非 Itcl 实例构造返回非限定名称,并且处理委托时的当前命名空间与制作 Tail 实例时不同.

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.

我的证据?这:

% info class instances Tail
::Dog::tail0

但它确实表明我们可以通过将 Dog 的定义更改为:

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]]
    }
}

有了这个,我们可以:

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

我觉得还行.

这篇关于如何在 TCL 中使用 itcl::delegation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆