New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PowerShell should support creating an List similar to how it supports arrays #5643
Comments
|
I like the idea but to truly impact performance you'd need to be operating on large lists. For a convenient way to create large lists, I would expect something like this to work |
|
Two points:
|
|
I like the idea of having a list literal in powershell. I think it could have a syntax like |
|
@lzybkr is it something like this? |
|
Sure, alias properties would be needed to make lists work just like arrays. |
If that's not considered too much of a breaking change, it would certainly be the best solution. Otherwise: I get what you're saying about large lists, but that's where In other words: something like the following would make sense: $al = @[] # simpler than: [System.Collections.ArrayList]::new()
for ($i = 0; $i -lt 1000; ++$i) {
$al += $i # simpler than: $null = $al.Add($i)
} |
|
I submitted two PRs (WIP) with different designs for the List support in PowerShell:
Without breaking change, I think the best we can do is probably to make At the same time, I started to think an alternative -- add I hope the those 2 PRs can draw more discussion on the design. |
|
@daxian-dbw since this code is beyond my understanding, does it attempt to create a strongly typed list, or is always |
|
@markekraus It attempts to always create |
|
@lzybkr proposed to use new token pairs instead of
It would be great to have |
|
@daxian-dbw: I'm really glad to see you take this on, but before we go any further with the syntax debate: Is the consensus that we cannot just simply switch The answer may well be that yes, it's too risky to make that change (I personally cannot tell), but if it happens to be no, after all, there's no need for a syntax debate. |
|
@mklement0 IMHO, there would be 3 problems if we simply change the comma operator
I prefer a |
|
@daxian-dbw Thanks for great prototypes!
I personally like |
|
|
|
|
|
Minor correction: Also, outside of the literals.. I like the idea of Lists getting an accelerator, but only if it works similar to |
|
I have only one question - where I can buy Unicode keyboard with 32000 buttons to replace my 102 keyboard? We could combine the accelerator idea and list literals: @[int](1,2,3)
@[string](dir C:\)
@[](1,2,3) as short cut of @[object](1,2,3) |
|
@daxian-dbw: Thanks for the detailed feedback. I can't speak to 1. (AST names), but perhaps the answer is to special-case The alternative is to simply make Again, it might be too risky, but it would solve the syntax problem. That said, that alone wouldn't address the desire for explicit typing. Perhaps the special casing could be tweaked to translate something like The need for the inner On the other hand, explicit typing is a more advanced use case, and optimizing for the typical case is arguably more important. |
I talked to @jpsnover about this today and he also brought up changing the semantic of
For (1), could it be OK to have this inconsistency? |
|
The Ast type name doesn't matter that much. There are many examples outside of PowerShell where the name can be misleading - Lua is another good example - quoting from here.
|
|
Actually, I would expect that to work with instances of any type that implements the
While slightly awkward, as discussed, |
|
I don't get the drive error you get. Are you testing that in a session that defines a I suppose I really like the notion that you can add a character to an enclosure prefix in your scripts and voilà, they'll use a more efficient data structure. That would be a very low cost performance enhancement for some scripts if the data structure was implemented properly with operator support for things like |
|
Just to put another alternative on the table:
That's shorter, but |
|
Nope, fresh PS7-preview1 session. Yeah, could do, but then you lose the callback to |
|
As Jason meantioned above lists is probably edge case for scripts - so no need to have a syntax suger for creating lists. Perhaps we could only enhance '+' (+=) operator to support lists and concurrent collections (other types?). |
|
That's definitely a no-brainer; we need the The syntactic sugar would really be nice as well though |
|
Please consider adding support for $MyArrayList = [System.Collections.ArrayList]@(0, 1, 3, 4)
$MyArrayList += 5
$MyArrayList.Insert(2, 2) # Exception calling "Insert" with "2" argument(s): "Collection was of a fixed size." |
|
I think we have an existing issue for that specifically: #5805 It came up again recently as a duplicate, but my comment there still stands: #13152 (comment) |
|
@daxian-dbw perhaps we can turn your |
|
@SteveL-MSFT clarification point on that -- would |
|
@vexx32 good question, I suppose it should probably match |
|
Has anyone mentioned exposing a $pscollectionprefererence variable where would could override what collection type is used natively, for all array operators and pipeline output? |
I'm late to but in PowerShell square brackets are also used for types: I'm all for |
|
Reviving this. Rather than introduce a new language syntax, I think it would be simpler to just introduce the proposed |
|
... If only that the implication of this syntactic sugar proposal will likely also support Constrained Language Mode. |
|
The type accelerator doesn't change behavior of related operators. |
|
I meant to comment on this thread but apparently never did. Specifically, regarding making With regards to That said, I did prototype (two years ago apparently) a As a side note on the topic of lists and their place in PowerShell, they aren't actually very frequently better than simply using |
|
Good point that using list types is often not needed, @SeeminglyScience. As for your "List<>-y" implementation. At least at first glance it sounds similar to what @PetSerAl - who agrees that Implementing our own list type that plays nicely with
The least-effort alternative, perhaps acceptable in light of the need for lists not being as pressing as it may seem, would be:
using namespace System.Collections.Generic
$list = [List[int]] 1..10 # !! WRONG
$list = [List[int]] (1..10) # OK
[List[int]] $list = 1..10 # OK |
Hah! They beat me to it by two years, that's amazing. Thanks for the link |


TravisEz13 commentedDec 6, 2017
•
edited
Powershell supports creating arrays with
$array = 'a', 1, '3'. Then you can add an element to the array with$array += 4, but this creates a new array which is not performant.Powershell should have a syntax which allows creating lists.
Assuming the operator is
@[...], you could create a list with$list = @['a', 1, '3']and then you could add an element to the existing list with$list += 4without PowerShell having to create a new list.Note: this new operator might function more like
@(...)This design assumes that changing
,would be a breaking change. I'm open to discussing changing,as well.I filed this based on an offline discussion about this comment on a PR: #5625 (comment)
The text was updated successfully, but these errors were encountered: