【问题标题】:One line if-condition-assignment in AutoHotkeyAutoHotkey 中的一行 if-condition-assignment
【发布时间】:2021-11-07 22:53:39
【问题描述】:

在 JavaScript 中,我们可以使用以下单行:

const condition = true

let foo

condition && (foo = 'foo') // ???? one-liner

console.log(foo) // foo

我在 AHK 中试过这个:

condition := true

condition && (foo := "foo")

MsgBox % foo

但是,解释器抛出:

我只好把上面的代码改成下面提示“foo”:

condition := true

; ???? three lines
if (condition) {
  foo := "foo"
}

MsgBox % foo

如何在 AHK 的一行中做这种类型的赋值?

【问题讨论】:

    标签: operators autohotkey variable-assignment assignment-operator short-circuiting


    【解决方案1】:

    我们可以使用ternary operator (?:) 来实现这一点,与 JavaScript 不同的是,我们可以省略 AutoHotkey 中的丢失分支:

    condition := true
    
    condition ? foo := "foo"
    
    MsgBox % foo
    

    另外,引用0x464e:

    请注意,如果您不尝试获取整个三元组的评估结果,则只能省略失败的分支,例如var1 := TrueCondition ? "hello" 不起作用。变量var1 没有设置任何值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-20
      相关资源
      最近更新 更多