相当于 tcl 中的#define?

发布于 2024-10-17 09:46:03 字数 77 浏览 9 评论 0原文

tcl中有没有相当于C++#define的命令?我已经看到了使用 proc 函数重载来实现“定义”的方法,只是想知道是否有人知道更直接的方法

Is there a command in tcl that is equivalent to C++ #define? I've seen ways to implement "define" using overloading of the proc function, just wanted to know if anyone knows of a more starightforward way

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

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

发布评论

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

评论(4

薄荷梦 2024-10-24 09:46:03

使用 interp alias 允许您在创建别名时使用 ab 的内容:

interp alias {} foo_ab {} foo $a $b

如果您需要使用调用时的值,您需要一个辅助过程:

proc foo_ab args {
    global a b
    uplevel 1 [list foo $a $b {*}$args]
    # Or this in older Tcl: uplevel 1 [list foo $a $b] $args
}

在 8.5 中,也可以使用别名和 apply 编写:

interp alias {} foo_ab {} apply {args {
    global a b
    uplevel 1 [list foo $a $b {*}$args]
}}

在 8.6 中,您可以使用 tailcall 进一步优化code>:

interp alias {} foo_ab {} apply {args {
    global a b
    tailcall foo $a $b {*}$args
}}

您还可以使用其他一些更肮脏的技巧,如下所示:

interp alias {} foo_ab {} namespace inscope :: {foo $a $b}

虽然这不是特别快,但它确实适用于所有 Tcl 8.* 版本。

The use of interp alias allows you to use the contents of a and b at the time the alias was created:

interp alias {} foo_ab {} foo $a $b

If you need to use the values at the time it is called, you need a helper procedure instead:

proc foo_ab args {
    global a b
    uplevel 1 [list foo $a $b {*}$args]
    # Or this in older Tcl: uplevel 1 [list foo $a $b] $args
}

In 8.5, this can also be written with aliases and apply:

interp alias {} foo_ab {} apply {args {
    global a b
    uplevel 1 [list foo $a $b {*}$args]
}}

In 8.6, you can optimize further by using tailcall:

interp alias {} foo_ab {} apply {args {
    global a b
    tailcall foo $a $b {*}$args
}}

You could also use some other, dirtier tricks like this:

interp alias {} foo_ab {} namespace inscope :: {foo $a $b}

That's not especially fast though, but it does work in all Tcl 8.* versions.

羁〃客ぐ 2024-10-24 09:46:03

Tcl 有一种机制可以让您定义过程的别名在口译员中。

如果你

proc foo {one two three} {do something with $one $two $three}

发现你总是传递 $a 和 $b 作为前两个参数,你可以这样写:

interp alias {} foo_ab {} foo $a $b

现在你可以说:

foo_ab $d   ;# same as "foo $a $b $d"
foo_ab $e   ;# same as "foo $a $b $e"

示例:

proc foo {one two three} {puts [join [list $one $two $three] :]}
set a Hello
set b World
interp alias {} foo_ab {} foo $a $b
foo_ab example  ;# prints "Hello:World:example"

interp alias 命令中的空大括号只是表示当前的解释器。你可以和奴隶翻译一起做很多有趣的事情。

Tcl has a mechanism that lets you define aliases to procedures in an interpreter.

If you have

proc foo {one two three} {do something with $one $two $three}

and you find you're always passing $a and $b as the first two arguments, you can write:

interp alias {} foo_ab {} foo $a $b

And now you can say:

foo_ab $d   ;# same as "foo $a $b $d"
foo_ab $e   ;# same as "foo $a $b $e"

example:

proc foo {one two three} {puts [join [list $one $two $three] :]}
set a Hello
set b World
interp alias {} foo_ab {} foo $a $b
foo_ab example  ;# prints "Hello:World:example"

The empty braces in the interp alias command merely signify the current interpreter. You can do lots of fun things with slave interpreters.

哀由 2024-10-24 09:46:03

或者,您可以将 proc 定义为期望 d 和 e 作为具有默认值(例如空字符串)的输入参数,例如,

proc foo {a b c {d ""} {e ""} }.....

如果您要拥有未知数量的输入参数,您可以使用单词 args,这将创建一个包含 args 中每个值的列表,例如

   proc foo {a b c args } {
     foreach bar $args {
       #do whatever...
     }
   }

欢呼
布莱恩

Alternatively you can define your proc to expect both d and e as input parameters with a default value (e.g. empty string) e.g.

proc foo {a b c {d ""} {e ""} }.....

If you're going to have an unkown number of input parameters you can use the word args, which will create a list containing each value in args e.g.

   proc foo {a b c args } {
     foreach bar $args {
       #do whatever...
     }
   }

cheers
Brian

A君 2024-10-24 09:46:03

如果“接收相同的参数”意味着您重复为 $a$b$c 传递相同的值,那么您的一种选择是使用全局变量而不是函数参数。在调用函数之前将值存储在其中,然后您的函数调用就会简化为 foo $d 等。

If by "receives the same arguments" you mean that you are repeatedly passing the same values for $a, $b, and $c, then one option you have is to use globals instead of function parameters. Store values in them before calling your function, and then your function call simplifies down to foo $d, etc.

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