The type and value of an empty pipeline

D

DioGenus

I would intuitively think that since the output from a pipeline normally is
an array, the "output" from an empty pipeline should be the empty array @().

E.g.

PS> ( @(1,2) | % { $_ } ).GetType()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array

is an array while

PS> ( @() | % { $_ } ) -eq $null
True

Can anyone explain why $null is a better choice than @()? In my scripts, I
now have to perform extra testing before I can add the output of a pipeline
to an existing array since the following statement would just add a $null
element to $myArray if $maybeEmptyArray is empty:

$myArray += @() + $maybeEmptyArray | % { doProcess $_ }

I obviously want nothing added to $myArray if $maybeEmptyArray is empty.
 

My Computer

S

stej

Yes, this is a little bit unfortunate.

But there is a quick fix:
$maybeEmpty = @( @() | % { $_ } )

so
@( @() | % { $_ } ).Length -eq 0

On May 21, 12:35 pm, DioGenus <[email protected]>
wrote:

> I would intuitively think that since the output from a pipeline normally is
> an array, the "output" from an empty pipeline should be the empty array @().
>
> E.g.
>
>     PS> ( @(1,2) | % { $_ } ).GetType()
>
>     IsPublic IsSerial Name                                     BaseType
>     -------- -------- ----                                     --------
>     True     True     Object[]                                 System.Array
>
> is an array while
>
>     PS> ( @() | % { $_ } ) -eq $null
>     True
>
> Can anyone explain why $null is a better choice than @()?  In my scripts, I
> now have to perform extra testing before I can add the output of a pipeline
> to an existing array since the following statement would just add  a $null
> element to $myArray if $maybeEmptyArray is empty:
>
>     $myArray += @() + $maybeEmptyArray | % { doProcess $_ }
>
> I obviously want nothing added to $myArray if $maybeEmptyArray is empty.
 

My Computer

D

DioGenus

Exactly what I needed! Thanks!!! I spent quite a bit of time hunting down
this "bug" in my code and truly appreciate that I don't have to add an
explicit test for each such array addition.

DioGenus

"stej" wrote:

> Yes, this is a little bit unfortunate.
>
> But there is a quick fix:
> $maybeEmpty = @( @() | % { $_ } )
>
> so
> @( @() | % { $_ } ).Length -eq 0
>
> On May 21, 12:35 pm, DioGenus <[email protected]>
> wrote:

> > I would intuitively think that since the output from a pipeline normally is
> > an array, the "output" from an empty pipeline should be the empty array @().
> >
> > E.g.
> >
> > PS> ( @(1,2) | % { $_ } ).GetType()
> >
> > IsPublic IsSerial Name BaseType
> > -------- -------- ---- --------
> > True True Object[] System.Array
> >
> > is an array while
> >
> > PS> ( @() | % { $_ } ) -eq $null
> > True
> >
> > Can anyone explain why $null is a better choice than @()? In my scripts, I
> > now have to perform extra testing before I can add the output of a pipeline
> > to an existing array since the following statement would just add a $null
> > element to $myArray if $maybeEmptyArray is empty:
> >
> > $myArray += @() + $maybeEmptyArray | % { doProcess $_ }
> >
> > I obviously want nothing added to $myArray if $maybeEmptyArray is empty.
>
> .
>
 

My Computer

Top