将 proc 变量导入命名空间

发布于 2024-08-08 11:46:22 字数 660 浏览 2 评论 0原文

proc foo {param} {
  namespace eval foo_ns {
     uplevel {set foo_ns::x $param }
  }
}

这看起来很丑。

[upvar] 将不起作用,因为它无法链接到“param”。

谢谢。

答案中的代码不起作用(tclsh8.4)

------------------------------------------------ ----

% proc bar {param} {
    namespace eval foo_ns {
      uplevel [list set foo_ns::x $param]
    }
  }
% bar 123
can't read "param": no such variable

----------------------------------------------------------

% proc foo {param} {
    set ::foo_ns::x $param
}
% foo 123
can't set "::foo_ns::x": parent namespace doesn't exist
proc foo {param} {
  namespace eval foo_ns {
     uplevel {set foo_ns::x $param }
  }
}

This just looks ugly.

[upvar] will not work, because it can't link to 'param'.

Thanks.

Code from answers does not work (tclsh8.4)

-------------------------------------------

% proc bar {param} {
    namespace eval foo_ns {
      uplevel [list set foo_ns::x $param]
    }
  }
% bar 123
can't read "param": no such variable

-------------------------------------------

% proc foo {param} {
    set ::foo_ns::x $param
}
% foo 123
can't set "::foo_ns::x": parent namespace doesn't exist

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

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

发布评论

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

评论(3

潇烟暮雨 2024-08-15 11:46:22

有什么问题:

proc foo {param} {
    set ::foo_ns::x $param
}

在我的测试中,它似乎实现了相同的目标。

更新:感谢 K0re 指出了这一点。在调用 foo 之前,需要定义名称空间:

namespace eval ::foo_ns {}

What is wrong with:

proc foo {param} {
    set ::foo_ns::x $param
}

In my test, it seems to accomplish the same objective.

Update: Thanks To K0re for pointing this out. Before calling foo, you need to define the name space:

namespace eval ::foo_ns {}
掐死时间 2024-08-15 11:46:22

命名空间和级别是两个不同的东西。你不需要升级来解决这个问题。

这是一个简单的解决方案,它创建命名空间并在一行中设置变量:

proc foo {param} {
    namespace eval ::foo_ns [list set x $param]
}

Namespaces and levels are two different things. You don't need uplevel for this problem.

Here's a simple solution that creates the namespace and sets the variable in one line:

proc foo {param} {
    namespace eval ::foo_ns [list set x $param]
}
执手闯天涯 2024-08-15 11:46:22

好吧,你有两个不同的问题。首先是命名空间尚不存在;第二个是您需要编写代码以便在该命名空间中创建/写入变量。总的来说,这只需要对 Hai 的代码进行微小的修改:

proc foo {param} {
    # Create the namespace if it doesn't already exist
    namespace eval ::foo_ns {}
    # Set the variable in the namespace
    set ::foo_ns::x $param
}

作为对您遇到的一些问题的评论:

proc foo {param} {
  namespace eval foo_ns {
     uplevel {set foo_ns::x $param }
  }
}

这不起作用,因为您实际上是在说以下内容:
在命名空间“foo_ns”中,运行以下代码:
在堆栈的顶层,运行以下代码:
“set foo::x $param”

但是,在堆栈的顶层,变量“param”没有值(它仅在过程中定义。您需要确保它事先被替换。我会包括可以工作的代码,但老实说,我担心它会导致与问题的实际答案混淆......所以我将其省略。

Ok, you have two different problems. The first is that the namespace doesn't already exist; the second is that you need to write the code so that the variable is created/written in that namespace. Overall, this require only a tiny modification of Hai's code:

proc foo {param} {
    # Create the namespace if it doesn't already exist
    namespace eval ::foo_ns {}
    # Set the variable in the namespace
    set ::foo_ns::x $param
}

As commentary on some of the problems you were having:

proc foo {param} {
  namespace eval foo_ns {
     uplevel {set foo_ns::x $param }
  }
}

This doesn't work because you are, effectively, saying the following:
in the namespace "foo_ns", run the following code:
at the top level of the stack, rung the following code:
"set foo::x $param"

However, at the top level of the stack, the variable "param" has no value (its only defined within the procedure. You'll need to make sure it gets substitured beforehand. I'd include code that would work but, honestly, I'm afraid it will cause confusion with the actual answer to the question... so I'll leave it out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文