“:”和“?”的含义

Meaning of ":" and "?"

我在 AutoIt 脚本的 Return 语句中发现了分号和问号:

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

Func A()
  ;do somethingA
EndFunc

Func B($a,$b,$c)
  ;do somethingB
EndFunc

Func C($a,$b,$c,$d)
  ;do somethingC
EndFunc

Func CallFunc( $f, $a = Default, $b = Default, $c = Default, $c = Default )
  Return FuncName($f) = "A" ? $f() : FuncName($f) = "B" ? $f($a,$b,$c) : $f($a,$b,$c,$d)
EndFunc

Example()

Func Example()
  CallFunc( A )
  CallFunc( B, 1, 2, 3 )
  CallFunc( C, 1, 2, 3, 4 )
EndFunc

我知道 FuncName() returns 存储在变量中的函数名称,但我不知道此 Return 语句中问号和分号的含义:

Return FuncName($f) = "A" ? $f() : FuncName($f) = "B" ? $f($a,$b,$c) : $f($a,$b,$c,$d)

I don't know question-marks & semicolons meanings

根据Documentation - Keywords - Ternary operator

Conditionally chooses one of two responses based on the result of an expression.

例如;

Return $g_bBlockInput ? $MOE_BLOCKDEFPROC : $MOE_RUNDEFPROC

在功能上等同于:

If $g_bBlockInput Then

    Return $MOE_BLOCKDEFPROC

Else

    Return $MOE_RUNDEFPROC

EndIf

所以

Return FuncName($f) = "A" ? $f() : FuncName($f) = "B" ? $f($a,$b,$c) : $f($a,$b,$c,$d)

等于:

If FuncName($f) = "A" Then

    Return $f()

Else

    If FuncName($f) = "B" Then

        Return $f($a,$b,$c)

    Else

        Return $f($a,$b,$c,$d)

    EndIf

EndIf

无论该代码的目的如何,似乎都适用于 Switch...Case...EndSwitch。三元运算符的流行用法包括条件赋值。示例:

Global Const $g_bState = True
Global Const $g_sState = $g_bState ? 'ON' : 'OFF'

ConsoleWrite('$g_bState = ' & $g_sState & @CRLF)