PowerShell functions return behavior

后端 未结 3 2243
失恋的感觉
失恋的感觉 2021-02-12 21:58

I am seeing some rather weird behavior with PowerShell, it looks like custom functions might need a \"parenthesis wrapper\" to evaluate as you might expect them. Given a simple

3条回答
  •  独厮守ぢ
    2021-02-12 22:35

    The second line is not doing a boolean evaluation. Look at what happens if you do the same thing with strings.

    PS C:\> function Return-True { return "True string" }
    PS C:\> Return-True
    True string
    PS C:\> Return-True -eq "False string"
    True string
    PS C:\> (Return-True) -eq "False string"
    False
    

    The second line is simply returning the value of the function, and not doing a comparison. I'm not sure exactly why this behavior is happening, but it makes the behavior easier to see than when using boolean values that are being converted to the strings "True" and "False".

提交回复
热议问题