[incr Tcl] 中的静态函数继承

发布于 2024-10-13 13:59:12 字数 562 浏览 6 评论 0原文

incr Tcl 中的继承无法按预期工作。考虑下面的代码。

package require Itcl

::itcl::class Base \
{
public {
    proc function { } { puts "==== Base::function" }
}
}

::itcl::class Derived { inherit Base }

Base::function
Derived::function    ;# FAILS

最后一行失败,因此 Base::function 不会在 Derived 处继承,尽管 Derived 继承自 Base

我做错了什么吗,还是 incr Tcl 的设计行为所以?

Inheritance in incr Tcl doesn't work as expected. Consider the code below.

package require Itcl

::itcl::class Base \
{
public {
    proc function { } { puts "==== Base::function" }
}
}

::itcl::class Derived { inherit Base }

Base::function
Derived::function    ;# FAILS

The last line fails, so Base::function is not inherited at Derived, though Derived inherits from Base.

Am I doing something wrong, or incr Tcl is designed to behave so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哽咽笑 2024-10-20 13:59:12

阅读文档我不认为 itcl 类中的过程按照您认为应该的方式工作:

过程名称?args? ?身体?
声明一个名为 name 的过程。 proc 是一个普通的过程
类的命名空间。与方法不同的是,
调用过程时不引用
一个特定的对象。当 proc 体
执行后,会自动产生
仅访问公共数据成员。
如果指定了 args 列表,它将建立以下的使用信息
这个过程主体命令可以是
用于重新定义过程体,但是
args 列表必须与此匹配
规格。
在另一个类方法或过程的主体内,可以调用过程
像任何其他命令一样 - 只需使用
它的名字。在任何其他命名空间中
上下文中,该过程是使用
限定名称,如“className::proc”。
基类中的过程是
在当前类中重新定义,或者
被另一个基类隐藏,也可以
通过他们的限定名称进行访问。

我对此的理解是,过程与其类相关联,可以在派生类中引用它,但没有在其中定义。例如以下作品:

package require Itcl

::itcl::class Base {
    public {
        proc function { } { puts "==== Base::function" }
    }
}

::itcl::class Derived { 
inherit Base 
    public {

        proc function { } { 
            puts "==== Derived::function"
            return [Base::function] 
        }
    }
}

Base::function
Derived::function    ;# FAILS

Reading the docs I don't think that procs in an itcl class work the way you think they ought to:

proc name ?args? ?body?
Declares a proc called name. A proc is an ordinary procedure within
the class namespace. Unlike a method,
a proc is invoked without referring to
a specific object. When the proc body
is executed, it will have automatic
access only to common data members.
If the args list is specified, it establishes the usage information for
this proc. The body command can be
used to redefine the proc body, but
the args list must match this
specification.
Within the body of another class method or proc, a proc can be invoked
like any other command-simply by using
its name. In any other namespace
context, the proc is invoked using a
qualified name like "className::proc".
Procs in a base class that are
redefined in the current class, or
hidden by another base class, can also
be accessed via their qualified name.

My reading of this is that the proc is associated with it's class, it can be referred to in the derived class but it isn't defined in it. For example the following works:

package require Itcl

::itcl::class Base {
    public {
        proc function { } { puts "==== Base::function" }
    }
}

::itcl::class Derived { 
inherit Base 
    public {

        proc function { } { 
            puts "==== Derived::function"
            return [Base::function] 
        }
    }
}

Base::function
Derived::function    ;# FAILS
回眸一笑 2024-10-20 13:59:12

您定义的Base::function 过程(或多或少)是命名空间Base 中的常规过程。当你在 Itcl 中继承时,你只是继承方法,而不是继承过程。在相关注释中,您不能从 Base 的实例调用 proc function,您必须像调用任何常规 proc 一样调用它。

itcl::class Base {
  public {
    proc function { } { puts "==== Base::function" }
  }
  public method test {} {
    $this function
  }
  public method test2 {} {
    function
  }
}

Base bb
bb test   ;# yields error: bad option "function"
bb test2  ;# works as expected

The proc you defined Base::function is (more or less) a regular proc in the namespace Base. When you inherit in Itcl, you just inherit methods, you don't inherit procs. In a related note, you cannot call the proc function from an instance of Base, you have to call it like any regular proc.

itcl::class Base {
  public {
    proc function { } { puts "==== Base::function" }
  }
  public method test {} {
    $this function
  }
  public method test2 {} {
    function
  }
}

Base bb
bb test   ;# yields error: bad option "function"
bb test2  ;# works as expected
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文