-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Support a Set literal syntax #3792
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
Comments
This comment was originally written by @seaneagan another reason: without this there is no way to create a "const" Set, so you are forced to use a List when you need a constant. |
I have run up against this problem also, and have wanted a const Set. I have used a map with null values instead. |
This comment was originally written by [email protected]
Yep, I just ran into this as well. There is a workaround however, better than using a List, I think: static final Set<String> units = new Set<String>.from(['em','pt','px','%']); |
Be careful exposing a normal set directly, as it's mutable. Consider UnmodifiableSetView This is where having literal syntax + const would be great. |
This comment was originally written by [email protected] In my case it's package-internal, so I'm no concerned. |
Removed this from the Later milestone. |
Removed Oldschool-Milestone-Later label. |
See issue #174. This is almost a duplicate,e xcept I am not yet convinced that a const set requires literal set syntax. |
This comment was originally written by @kaendfinger If this were to come, I was thinking a prefix to the list literal: const Set<String> strings = @[ Prefixes that would maybe work would be: %, :, and ~ The @ prefix was just a demonstration, I don't recommend that one because of annotations (metadata). |
This comment was originally written by @kaendfinger Oops, add a const in front of the list literal in the above comment's example. |
I propose the curly bracket syntax for sets -- just same as maps. The only difference would be that brackes would contain particular elements instead of key-value pairs like this:
Is It seems logical in many ways:
|
👍 |
@VanNiewelt
. 👍 |
One problem with that is, how you define an empty set? |
EDN (Clojure data format https://github.com/edn-format/edn) uses this syntax #{1 2 3} It would also be cool if .add and .addAll would return a Set for better chaining |
I wonder if in most cases we could infer whether Side note:
Since chaining does not 'use up' the return value, the return value can be something useful. Set.add returns if (items.add(item)) {
print('Yay, a different item');
} |
All non-empty set literals would be distinguishable from map literals, as would any set literal with a type argument. The only tricky case is the |
Updated issue with links to more proposals: dart-lang/language#36 |
Fixed in dart-lang/language#37 |
In swift a empty dictionary (map) literal is written as |
An empty set is |
Okay, that makes sense, but will there also be a |
I don't think they will be particularly useful. If you want to document that a specific empty constant set has element type |
Okay, thanks for clarifying, that definitely makes sense and is also more readable than something like |
Takes a while to think about everything. Surely we didn't. 😄 |
Sets are very helpful, it would be nice to have a set literal syntax (like the [] for lists and {} for maps) for more terse code.
The text was updated successfully, but these errors were encountered: