如何使用变量作为TCL proc参数的默认值

| 我有一个变量,希望将其用作参数的默认值:
proc log {message {output $::output}} {
   ....
}
有没有办法做到这一点,或者需要我在proc中求值变量?     
已邀请:
        是的,但是您不能在参数列表中使用花括号(
{}
)。您声明程序例如这条路:
proc log [list message [list output $::output]] {
   ....
}
但请注意: 在声明过程时(而不是在执行过程时)评估变量!     
        如果要使用仅在调用时在value中定义的默认参数,则必须更加棘手。关键是可以使用
info level 0
获取当前过程调用的参数列表,然后只需检查该列表的长度即可:
proc log {message {output \"\"}} {
    if {[llength [info level 0]] < 3} {
        set output $::output
    }
    ...
}
请记住,在检查参数列表时,第一个是命令本身的名称。     
        另一种方法是:
proc log {message {output \"\"}} {
    if {$output eq \"\"} {
        set output $::output
    }
}
    

要回复问题请先登录注册