有什么方法可以在 Tcl 命令中嵌入注释吗?
我想在命令中添加注释,但考虑到“#”字符在 Tcl 8.4 中定义为:
如果散列字符(“#”)出现在 Tcl 的位置,则 这似乎是不可能的期望命令第一个单词的第一个字符,然后散列字符及其后面的字符,直到下一个换行符,将被视为注释并被忽略。注释字符仅当出现在命令开头时才有意义。
想象一下这个示例,说明它是如何工作的(这些注释在我的实验中都不起作用):
array set myArray [list red 3 \
blue 4 ;# Blue is before purple.
purple 5 # Purple is after red.
green 7 \
yellow 8]
似乎棘手的部分是如何在嵌入注释的情况下继续列表命令?也许类似于 C++ 风格的 /* 嵌入注释。 */ 但我只看到 # 在 Tcl 中用于注释行尾,没有用于开始和结束注释语法。
I would like to have a comment within a command and it appears that this is not possible given that the '#' character is defined in Tcl 8.4 to be:
If a hash character (``#'') appears at a point where Tcl is expecting the first character of the first word of a command, then the hash character and the characters that follow it, up through the next newline, are treated as a comment and ignored. The comment character only has significance when it appears at the beginning of a command.
Imagine this example of how this might work (none of these comments worked in my experiments):
array set myArray [list red 3 \
blue 4 ;# Blue is before purple.
purple 5 # Purple is after red.
green 7 \
yellow 8]
Seems the tricky part is how to continue the list command with a comment embedded? Perhaps something like the C++ style of /* Embedded comment here. */ but I only see # as being used in Tcl for comments to end of line, nothing for begin and end comment syntax.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不可以,您不能在命令调用中嵌入注释。 Tcl 中的注释与其他语言中的注释工作方式不太一样。有些人会被这个问题绊倒,大多数有经验的 Tcl 程序员都不会再考虑一下。
在极少数情况下,您确实需要这样做,通常可以很容易地解决它。使用您的示例:
您可能认为这比在一行上完成所有操作要慢,但除了最关键的时间情况外,在所有情况下差异都可以忽略不计,可能只有几微秒。
No, you cannot embed a comment within the invocation of a command. Comments in Tcl don't quite work the same way as they do in other languages. Some people stumble over this, most experienced Tcl programmers don't give it a second thought.
The rare times that you truly need to do this you can usually work around it easily enough. Using your example:
You might think this is slower than doing it all on one line but the difference is negligible in all but the most time-critical situations, probably on the order of just a few microseconds.
是的,有一种方法可以将注释嵌入到命令中。这并不漂亮,但这是可能的。添加仅包含列表成员注释的命令替换,如下所示(注释后的换行符是强制性的):
Yes, there is a way to embed a comment into a command. It's not pretty, but it's possible. Add a command substitution containing only a comment to a list member, like this (the newline after the comment is mandatory):