Reputation: 16236
I made a mistake. I used a FULL STOP between array values rather than a COMMA. What is $x? It appears to be $null. What does this expression mean? Why no error message?
PS 18:48 C:\src\t
>$x = 1.2.3
PS 18:48 C:\src\t
>$x
PS 18:48 C:\src\t
>$x.GetType()
You cannot call a method on a null-valued expression.
At line:1 char:1
+ $x.GetType()
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
PS 18:48 C:\src\t
>$null -eq $x
True
PS 18:48 C:\src\t
>$x.Count
0
Upvotes: 2
Views: 66
Reputation: 437638
To complement Paul G's helpful answer, which explains that the .3
in 1.2.3
is interpreted as accessing a property named 3
on the [double]
value that 1.2
is parsed as:
Why no error message?
By default, PowerShell ignores attempts to access non-existent properties on an object and returns $null
.
If you use Set-StrictMode -Version 2
or higher, however, a statement-terminating error occurs:
PS> Set-StrictMode -Version 2; 1.2.3
The property '3' cannot be found on this object. Verify that the property exists.
...
See the Set-StrictMode
help topic for more information.
While Set-StrictMode
is useful for preventing errors such as misspelled variable and property names, it has notable pitfalls:
Set-StrictMode
is dynamically rather than lexically scoped, which means that all code invoked from a scope in which it is in effect is also affected - and such code may not have been designed for that and therefore break; providing a lexically scoped strict mode to remedy that is the subject of this RFC.
with Set-StrictMode -Version 2
or higher, the implicitly added .Count
property that unifies PowerShell's handling of scalars and collections is then not available - this should be considered a bug, and has been reported in GitHub issue #2798.
Upvotes: 3
Reputation: 1229
1.2 is implied as an double type. the second "." tries to invoke a member (either property or method) from the double, since there is no member of an double named "3", $x becomes $null.
Upvotes: 3