-
Notifications
You must be signed in to change notification settings - Fork 213
Dart should have convenient syntax for creating (constant) sets. #36
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
Puts on pedantic hat. This issue really is a description of a solution (set literal syntax) and not a problem ("no way to create const sets"). |
If we had variadic arguments, and could write const set = Set('Lasse', 'Bob', 'Leaf'); For literals, I'd even be fine with re-using the list-literal syntax: const set = Set['Lasse', 'Bob', 'Leaf']; |
I'd argue that even lack of const sets is not the problem. |
I'd prefer if just |
The If we have rest arguments, then Special syntax is the most fundamental kind of compiler magic (aka. "the language"), so a magic
It can be confusing to use the same syntax for non-similar things (if we get spreads in collection literals, but not in function arguments, would we have spreads in "set literals"?), but that argument also applies to using The Pedantry: Well spotted. Changed title. |
Maybe we could allow forms like |
What is the ROI for this feature? Is the syntax change worth it if all we're trying to do is fix a handful of call sites? |
There are no constant sets today, that's certainly a thing that you couldn't achieve without a language change, and that might be quite convenient if you want to use a set in a tight loop and the alternative is otherwise to (1) create a suitable set at top level or as a static variable and then use it only in that loop, or (2) create the set dynamically each time round in the loop. |
I think I've seen @munificent or @lrhn mention having
|
@eernstg these are good hypothetical situations where it might be useful, but they are hypothetical. Do we have data showing that this change will have high enough impact to spend time on it right now? My intuition tells me there are areas with much greater impact than set syntax. |
For the record, my 👎 for this request should be interpreted as "we should not work on this now due to relatively low impact". The feature is great otherwise. |
FWIW, not having this feature has definitely been felt in Flutter land. We've designed entire APIs around the lack of this feature (e.g. the text decoration styles).
We could also extend the definition of "const" such that it could do something with it... (my ideal long-term solution here is that all code could be "const" except for code that has side-effects beyond allocating memory, like code that references globals, or that does I/O.) |
There is currently one proposal. Unless we find a better one, it will win by default. The disadvantage of #37 is the complexity for the edge case where we need to use the context type to distinguish the possible meanings of The only realist alternative suggested so far is to use The primary argument against this syntax is that it's inconsistent with other literals, that it doesn't generalize to other literals (we can't do it for The So, the tradeoffs I see are:
By "consistent" I mean that the syntax is similar to other similar constructs in the language. The With "generalizes" I mean that the syntax can be made consistent by generalizing it to other similar problem areas. That is, can the same syntax be used to solve other problems. The "avoids ambiguity" means that the syntax is not ambiguous, it doesn't introduce grammatically similar syntax meaning different things in a way that has to be disambiguated explicitly in the specification. The Some points in here can probable be argued about (the precise answer would perhaps be "yes(ish)" or "mostly" in some cases), but if we are planning varargs, then we should probably consider the |
That looks an awful lot like One idea I've toyed with is syntax like this: var numbers = Set.[1, 2, 3];
db.insert.{
name: "jane",
id: 1234
}; So C#'s collection initializer syntax instead translates similar syntax to a series of mutating calls on the object. That has its pros and cons. It avoids the duplicate allocation, but it forces the underlying object to be mutable. |
How about introducing a prefix for collection literal, similarly to how we currently do with raw strings ( final foo = s[23, 45]; // s as in Set
final bar = @[23, 45]; // just a non-letter symbol
final bar = <int>@[23, 45]; // with types |
final foo = s[23, 45]; // s as in Set This would be ambiguous if you used it to create a single-element set: final foo = s[23]; At that point, it clashes with the existing syntax to call the index operator on final bar = @[23, 45]; // just a non-letter symbol This would work, but I think it would be confusing for users since |
Silly question: Is there a compelling reason to have |
See #53, which proposes exactly that. The main limitation is that we don't actually have variadic arguments in the language. |
See also previous discussion at dart-lang/sdk#3792 |
Closing as set literal shipped in Dart 2.2 |
We have decided to move forward with #37 as a solution to this issue.
Dart has list literals and map literals, but no set literals.
Sometimes a set is exactly the type you want (an efficient
contains
check), but there is no nice syntax for creating a set, and especially no way to make a constant set.For APIs that take sets as arguments, this prevents them from having const constructors.
(The workaround would be to have an inefficient, or lazily built, set constructor based on a list literal:
const ConstSet([1, 2, 3])
, or a set constructor based on map keys:const ConstSet({1: 0, 2: 0, 3: 0})
. Neither is very usable or readable, and if they are at all reasonable, they can also be used as the implementation of a language-based set literal).Proposed solutions:
The text was updated successfully, but these errors were encountered: