Powershell uses multiple bool caches inconsistently - by Derp McDerp

Status : 

 


1
0
Sign in
to vote
ID 808193 Comments
Status Active Workarounds
Type Bug Repros 0
Opened 11/10/2013 2:47:50 AM
Access Restriction Public

Description

Powershell (v4 at least) uses multiple bool object caches and inconsistently:

$true | Add-Member NoteProperty a 1
(1 -eq 1) | Add-Member NoteProperty b 1
('a' -match 'a') | Add-Member NoteProperty c 1
($null -ne 1) | Add-Member NoteProperty d 1
('1' -gt '0') | Add-Member NoteProperty e 1

See if you can predict what the following will print out:

$true | gm a,b,c,d,e
[bool]'true' | gm a,b,c,d,e
(1 -gt 0) | gm a,b,c,d,e
[bool](1 -gt 0) | gm a,b,c,d,e
('a' -like 'a') | gm a,b,c,d,e
($null -eq $null) | gm a,b,c,d,e
($null -ne 1) | gm a,b,c,d,e
('1' -gt '0') | gm a,b,c,d,e
[bool]1 | gm a,b,c,d,e
($true -eq (1 -eq 1)) | gm a,b,c,d,e
Sign in to post a comment.
Posted by Derp McDerp on 1/2/2014 at 8:11 PM
Another example:

System.Management.Automation.ParserOps._TrueObject
System.Management.Automation.ParserOps._FalseObject
Posted by Derp McDerp on 1/2/2014 at 7:06 PM
Here's an example:

System.Management.Automation.Boxed.True
System.Management.Automation.Boxed.False


System.Management.Automation.Interpreter.ScriptingRuntimeHelpers.True
System.Management.Automation.Interpreter.ScriptingRuntimeHelpers.False