| |
Logical Operators
SKILL considers nil as FALSE and any other value as TRUE. The and (&&) and or (||) operators only evaluate their second argument if they need to determine the return result.
SKILL将nil视为假(FALSE),而将其他任何值视为真(TRUE)。而逻辑与(&&)和逻辑或(||)运算符只在需要确定返回结果时才评估它们的第二个参数。
Sample Logical Operators
Operator Arguments Function Example Return Value
&& general and 3 && 5 5
5 && 3 3
t && nil nil
nil&&t nil
|| general or 3||5 3
5||3 5
t||nil t
nil||t t
The && and || operators return the value last computed. Consequently, both && and || operators can be used to avoid cumbersome if or when expressions.
"&&" 和 "||" 运算符返回最后计算的值。因此,"&&" 和 "||" 运算符可以用来避免冗长的 if 或 when 表达式
consequently [ˈkɒnsɪkwəntli] adv 因此; 所以;
avoid [əˈvɔɪd] vt 避免; 防止; 回避; 避开; 躲避;
If possible,you want to avoid alcohol.
cumbersome [ˈkʌmbəsəm] adj 大而笨重的; 累赘的; 难以携带的; 缓慢复杂的; 冗长的; 复杂的;
Using &&
When SKILL creates a variable, it gives the variable a value of unbound to indicate that the variable has not been
initialized yet. 当 SKILL 创建一个变量时,它将变量的值设置为 "unbound",表示该变量尚未初始化。
Use the boundp function to determine whether a variable is bound. The boundp function
可以使用 boundp 函数来判断变量是否已绑定。boundp 函数具有以下行为:
■ Returns t if the variable is bound to a value.
■ Returns nil if it is not bound to a value.
Suppose you want to return the value of a variable trMessages. If trMessages is unbound, retrieving the value causes an error. Instead, use the expression
假设您想要返回变量 trMessages 的值。如果 trMessages 是未绑定状态,获取其值会导致错误。取而代之,可以使用下面的表达式:
boundp( 'trMessages ) && trMessages
Using ||
Suppose you have a default name, such as noName. Suppose you have a variable, such as userName. To use the default name if userName is nil, use the following expression
假设您有一个默认名称,比如 noName。假设您有一个变量 userName。如果要在 userName 为 nil 的情况下使用默认名称,请使用以下表达式:
userName || "noName"