Хорошая ловушка!Вот код с пояснениями в комментариях, и функция также показывает свои аргументы:
function ReturnTrue()
{
write-host "ReturnTrue executed with $args!"
return $true
}
# this invokes ReturnTrue once with arguments: -and ReturnTrue -and ReturnTrue
if (ReturnTrue -and ReturnTrue -and ReturnTrue)
{
write-host "ReturnTrue should have executed 1 time"
}
# this invokes ReturnTrue 3 times
if ((ReturnTrue) -and (ReturnTrue) -and (ReturnTrue))
{
write-host "ReturnTrue should have executed 3 times"
}
Выход:
ReturnTrue executed with -and ReturnTrue -and ReturnTrue!
ReturnTrue should have executed 1 time
ReturnTrue executed with !
ReturnTrue executed with !
ReturnTrue executed with !
ReturnTrue should have executed 3 times