Debugging Type Binding in PowerShell
Some effort I spent in researching Type Binding in Stack Overflow to help answer a question by Chris Oldwood helped me solidify my understanding of the best way to debug more complicated scenarios such as this in PowerShell.
Spent some digging into this and this is what I’ve observed.
First for clarity I do not believe that you should consider the NullString value the same as null in a basic comparison. Not sure why you need this either, as this is normally something I’d expect from c# development. You should be able to just use $null
for most work in PowerShell.
|
|
Secondly, the issue is not necessarily because of the call operator, ie &
. I believe instead you are dealing with underlying parameter binding coercion. Strong data typing is definitely a weak area for PowerShell, as even explicitly declared [int]$val
could end up being set to a string type by PowerShell automatically in the next line when writing Write-Host $Val
.
To identify the underlying behavior, I used the Trace-Command
function (Trace Command) .
I changed the Use-Dot to just call the function as no write-host was needed to output the string.
|
|
The Format-Type I modified to also use what is considered a better practice of $null
on the left, again due to type inference.
|
|
To narrow down the issue with the data types, I used the following commands, though this is not where I found insight into the issue. Theyh when called directly worked the same.
|
|
However, when I ran the functions using TypeConversion tracing, it showed a difference in the conversions that likely explains some of your observed behavior.
|
|
|
|
OUTPUT: (null)
|
|
OUTPUT: System.String
The noticeable difference is in Use-Ampersand
it shows a statement of Converting null to ""
vs Converting "" to "System.Object"
.
In PowerShell, $null <> [string]''
. An empty string comparison will pass the null check, resulting in the success of outputting GetType()
.
A Few Thoughts On Approach With PowerShell
Why it’s doing this, I’m not certain, but before you invest more time researching, let me provide one piece of advice based on learning the hard way.
If start dealing with issues due to trying to coerce data types in PowerShell, first consider if PowerShell is the right tool for the job
Yes, you can use type extensions. Yes, you can use .NET data types like $List = [System.Collections.Generic.List[string]]::new()
and some .NET typed rules can be enforced. However, PowerShell is not designed to be a strongly typed language like C#. Trying to approach it like this will result in a many difficulties. While I’m a huge fan of PowerShell, I’ve learned to recognize that it’s flexibility should be appreciated, and it’s limits respected.
If I really had issues that required mapping [System.Management.Automation.Language.NullString]::Value
so strongly, I’d consider my approach.
That said, this was a challenging investigation that I had to take a swing at, while providing my 10 cents afterwards.
Other Resources
After posting my answer, I found another answer that seemed relevant, and also backs up the mentioning of not using [NullString]
normally, as its usage in PowerShell is not really what it was designed for.
Stackoverflow specific content republished under CC-BY-SA
Webmentions
(No webmentions yet.)