如何理解Tcl的return

huangapple go评论50阅读模式
英文:

How to understand tcl's return

问题

当我在C环境中执行以下语句时,

string tclCmd = "info args " + tclProcName + ";return";
    Tcl_Eval(interp, tclCmd.c_str());
    string res = Tcl_GetStringResult(interp);

执行的结果没有在tclsh中打印出来,但同时也无法获得正确的值。

所以我尝试像这样编写:

string tclCmd = "info args " + tclProcName;
    Tcl_Eval(interp, tclCmd.c_str());
    string res = Tcl_GetStringResult(interp);
    Tcl_Eval(interp, "return");

这样做是有效的,但我不明白为什么当执行Tcl_Eval时没有立即打印出来,并且return语句仍然有效。

英文:

I wanted to get the number of parameters of a proc from tcl and didn't want it to be printed in tclsh, so I used return

When I execute the following statement in the c environment

string tclCmd = "info args " + tclProcName + ";return";
    Tcl_Eval(interp, tclCmd.c_str());
    string res = Tcl_GetStringResult(interp);

The result of the execution is not printed in tclsh, but at the same time it is not possible to get the correct value

So I tried to write it like this:

string tclCmd = "info args " + tclProcName;
    Tcl_Eval(interp, tclCmd.c_str());
    string res = Tcl_GetStringResult(interp);
    Tcl_Eval(interp, "return");

This works, but I don't understand why it doesn't print out immediately when Tcl_Eval is executed, and the return statement is still valid afterwards

答案1

得分: 0

return 命令在这里一点帮助都没有。它触发了一个异常条件,导致 Tcl_Eval 的返回代码为 TCL_RETURN 而不是 TCL_OK(如果相关的话,会由过程的外部结构转换)。相反,你应该在完成返回值的处理之后(或者获取了其他引用)调用 Tcl_ResetResult(interp);,以将事务恢复到在调用你的命令实现之前的状态。

英文:

The return command is not helpful at all there. It triggers an exception condition that comes out of Tcl_Eval as a return code of TCL_RETURN instead of TCL_OK (that is converted by the outer structure of a procedure, if that is relevant). Instead, you should call Tcl_ResetResult(interp); after you have finished with the return value (or have taken another reference to it) to put things back to how they were before your command implementation was invoked.

huangapple
  • 本文由 发表于 2023年2月8日 14:35:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定