Home Dashboard Directory Help
Search

PS3 Length property added to all objects should be PSCount by Joel 'Jaykul' Bennett


Status: 

Active


14
0
Sign in
to vote
Type: Bug
ID: 793560
Opened: 7/11/2013 11:57:22 AM
Access Restriction: Public
0
Workaround(s)
view
2
User(s) can reproduce this bug

Description


In PowerShell v3 the "Length" property was added to ALL objects. This was wrong. It should have been a new PSCount or PSLength property, because the way it is now it gets in the way of all sorts of scenarios where objects are in heterogeneous collections of objects where some have Length and others don't, and you want to use that property. For instance, when we have folders and files in a single object array or pipeline stream, which happens all the time.

It also causes very weird behavior where the simplified syntax for ForEach-Object, and Where-Object produce different results than the normal syntax.

I understand that the intent was to provide a .Length = 1 that could be tested to see how many items were returned from a command, but the choice of "Length" was wrong, and it really needs to be removed because it causes more harm than good.

"Count" is somewhat more appropriate, but "PSCount" would have been much better.

Details
Sign in to post a comment.
Posted by Derp McDerp on 12/20/2013 at 6:27 PM
I think a better solution would be to add "keyword members" to the language analogous to C#'s extension methods. e.g.

$foo.-length

runs the keyword pseudo-member -length on $foo. This has the benefit of:

1. not clashing with anything (e.g. any real -length property can only be accessed via $foo.'-length' or $foo['-length'])
2. resolves statically at compile time
3. works with strict mode
Sign in to post a workaround.