Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - strange PowerShell pipe semantics

Reply
 
Old 03-18-2007   #1 (permalink)
Jason Mobarak


 
 

strange PowerShell pipe semantics

Hello --

Can anyone explain why PowerShell has these semantics:

---8<---

function Write-Type ($o) {
if ( $o -eq $null ) {
Write-Host "Null"
} else {
Write-Host $o.GetType().Name
}
}

$list = @()
Write-Type $list # Object[]

$result = $list | %{ $_ }
Write-Type $result # Null

$list = @(1)
Write-Type $list # Object[]

$result = $list | %{ $_ }
Write-Type $result # Int32

$list = @(1,2)
Write-Type $list # Object[]

$result = $list | %{ $_ }
Write-Type $result # Object[]

--->8---

....I would argue that an operation on a list should result in a list --
unless it's specifically a conversion operation. I know how to force
PowerShell to return a list but the semantics of the above seem strange,
why would PowerShell return different types based on the contents of the
list?

Thanks,
-- Jason

My System SpecsSystem Spec
Old 03-18-2007   #2 (permalink)
Andrew Savinykh


 
 

Re: strange PowerShell pipe semantics

Mainly because powershell is targeted against administrators and not
programers. When you are administring somethiing, it's considered to be
more natural not to force user to put array operator if only one value
returned. Remember, that it's assumed that you are entering your
commands from the command line, and the goal is to let you type as
little as possible. Similar reason for returning $null and not an empty
array. This way the result can be easily used in a conditional
expression, because $null is interpreted as $false.

Please note that I'm not advocating this approach, I'm just passing on
the knowledge =)

Jason Mobarak wrote:
> Hello --
>
> Can anyone explain why PowerShell has these semantics:
>
> ---8<---
>
> function Write-Type ($o) {
> if ( $o -eq $null ) {
> Write-Host "Null"
> } else {
> Write-Host $o.GetType().Name
> }
> }
>
> $list = @()
> Write-Type $list # Object[]
>
> $result = $list | %{ $_ }
> Write-Type $result # Null
>
> $list = @(1)
> Write-Type $list # Object[]
>
> $result = $list | %{ $_ }
> Write-Type $result # Int32
>
> $list = @(1,2)
> Write-Type $list # Object[]
>
> $result = $list | %{ $_ }
> Write-Type $result # Object[]
>
> --->8---
>
> ...I would argue that an operation on a list should result in a list --
> unless it's specifically a conversion operation. I know how to force
> PowerShell to return a list but the semantics of the above seem strange,
> why would PowerShell return different types based on the contents of the
> list?
>
> Thanks,
> -- Jason

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Powershell Pipe to Foreach to get indiviudal elements in a string collection. PowerShell
Using pipe in Powershell PowerShell
pipe to non-powershell process PowerShell
PowerShell: how to read Pipe.Input from a script? PowerShell
PowerShell: how to read Pipe.Input from a script? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46