2

Consider these two statements:

$result = [bool]$SomeObject
$result = if ($SomeObject) { $true } else { $false }

These two statements seem equivalent, but I am often surprised by PowerShell's quirks. Normally I'd try to work this kind of thing out from a language specification, but the closest thing to that seems to be stackoverflow.

  1. Are the above two statements equivalent for all possible types and values of $SomeObject?

  2. Is the answer to (1) actually baked into the CLR? If so, how can I tell that is the case?

alx9r
  • 3,675
  • 4
  • 26
  • 55
  • 2
    There is, in fact, a [PowerShell language spec](https://www.microsoft.com/en-us/download/details.aspx?id=36389). However, I have not seen one past version 3.0 and we are coming on version 5 soon. – Mike Zboray May 07 '15 at 03:54
  • How would the equivalence of any two statements in another language be "baked into" the CLR? – Mike Zboray May 07 '15 at 03:56
  • Wow. I stand corrected. I wonder why I've never seen that document referenced before. – alx9r May 07 '15 at 03:58
  • @mikez I don't know. I'm really asking this from a position of ignorance about how the CLR and Powershell work together, so (2) might just be a dumb question. – alx9r May 07 '15 at 04:05
  • 2
    FWIW, I tried writing some C# classes with user-defined conversions to boolean and it doesn't look like PowerShell considers user-defined conversions in either case. I suspect there is short list of built-in conversions and they are used for both cases. – Mike Zboray May 07 '15 at 04:24

0 Answers0