arrays - Why does $A+$B and $A,$B interact differently with Test-Path -


i have 2 arrays $a , $b both potentially empty.

$a = $b = @() 

this works:

$a+$b | test-path 

this not work:

$a,$b | test-path 

and returns error:

test-path : cannot bind argument parameter 'path' because empty array.

i have expected both expressions fail, + operator adding 1 empty array another, meaning resulting array still empty?

looking @ overall types of both methods shows same type.

ps y:\> $e = $a+$b ps y:\> $e.gettype()  ispublic isserial name                                     basetype -------- -------- ----                                     -------- true     true     object[]                                 system.array  ps y:\> $f = $a,$b ps y:\> $f.gettype()  ispublic isserial name                                     basetype -------- -------- ----                                     -------- true     true     object[]                                 system.array 

so why $a+$b & $a,$b interact differently test-path?

$a+$b | ... concatenates $a , $b before passing resulting array pipeline. pipeline unrolls (still empty) array, $null , test-path never called.

$a,$b | ... constructs array 2 nested arrays before passing pipeline. pipeline unrolls outer array , feeds each element (the empty arrays $a , $b) test-path, causing error observed.

basically you're doing $a+$b → @() in former, , $a,$b → @(@(), @()) in latter case.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -