-
Notifications
You must be signed in to change notification settings - Fork 213
Set literal #37
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
It has been brought up that this proposal intercepts with another proposed future feature: Collection spreads which allows you to write If there is a useful context type, then we can still use that to determine whether it's a set or map literal. If the literal is empty (zero spreads) it still needs to be a map. With no context type, that also cannot be an error. If there is no useful context type and at least one spread, then we can either give up (make it a compile-time error) or use the static type of the spread expressions to determine whether it's a set or map literal. Giving up is a valid and safe option - you want to write Or we can look at the static type of The complication here is that the static type of void foo<T>(Set<T> set) {}
main() {
foo({... [1, 2]}); // context type of `[1, 2]` is Iterable<?>, it gets filled out with <int> by the list.
} So, if we try to find the static type of the spread expressions, we do it with no context type, which is still possible, but may give a different result than what we would get if we had written the inferred type explicitly. I see three options:
The last case generalizes the default handling of the completely empty An example of a pitfall using any of the above cases (assume it's generated code, not just me being randomly silly): var setUnion = {...{1, 2}, ...{}, ...{5}}; With no context type, the I'm leaning towards the second option - use the static type of the spread expressions, and if that gives a unique answer which ensures the code will compile, use that. If it doesn't give an answer, or doesn't give a unique answer, then make it an error. We will not make a guess that will only be proven wrong at run-time. Another example to consider: var x = {...{...{...{}}}}; This is a map. There is no context type on the way down, so the inner (Also, note to self: Never make |
Here's an attempt to keep it simple. The rule of thumb would be "When we have evidence for a set and no evidence for a map, it will be a set. Otherwise, it will be a map". This means that we have no ambiguity error, all ambiguous forms will be checked assuming that they specify a map, and we may then get errors because they are half-and-half, but the point is that we have a consistent and backward compatible default: "Otherwise, it will be a map". In more detail, This means that we require unambiguous evidence for sets (noting "Never make Note that I'm using 'subtype' rather than 'assignable', because the latter will always apply for I think this is a rule which is easy to remember and understand, and this means that we don't need to cook up any complex ways to determine whether |
I don't actually think that makes it simpler. It provides a simple system, but you have to remember and understand the system. I still think that assuming If you write: var x = { ... dynamicExpression }; we would then accept and compile that as a map literal, and potentially fail at run-time if I think the user would be better helped by us flagging the ambiguity at compile-time and refusing to continue. The example: var x = { ... listAndMapExpression }; where the static type implements both I prefer not to guess at compile-time. If there is only one valid interpretation, pick that. If there is more than one (or definitely if there is less than one), fail visibly and early. I think this is actually easy for users to understand: When it works, it works in the way they expect it to (they gave enough hints about what they meant). When it doesn't work, they are told early and can give the necessary hint. At no time do we get to run-time with an interpretation other than what they expected (except perhaps the empty set, where our hands are tied). |
Right, the strict approach (where anything which is ambiguous is simply an error) is safer, in the sense that it will allow developers to catch unintended ambiguities. The trade-off is that things like Other than that, I can live with any of these proposals. ;-) |
I definitely agree with the earlier comment that we should avoid magic and be predictable. I wish there was some syntactic way we could be 100% unambiguous. With types given, it's unambiguous: |
About syntax we could also use a prefix (like raw string) : |
The |
Me too. One option is |
We have now added a more precise feature specification based on the original design proposal. Please take a look at this specification, and give feedback if it is unclear or if you anticipate any implementation issues. If there are no objections, we will schedule the design for implementation in Q4. @kmillikin @sigmundch @a-siva @mraleph @vsmenon @johnniwinther @munificent @danrubel @jwren @jcollins-g |
Hi, thanks! Set literals will be a nice feature.
Examples seems to be missing code quotes starting with "var v16", making
them hard to read.
It would also be great if, for those of us who don't read language
specifications every day, the examples section would indicate
interpretations in Dart prior to _and_ after the feature (even if "prior
to" means "compilation error"). This will help to highlight where the
differences are in practical terms. Besides being a sort of TL;DR of the
feature specification, it'll also help verify that my understanding of what
you wrote in the specification matches your intention.
Janice
…On Fri, Nov 2, 2018 at 5:14 AM Lasse R.H. Nielsen ***@***.***> wrote:
We have now added a more precise feature specification
<https://github.com/dart-lang/language/blob/master/accepted/future-releases/set-literals/feature-specification.md>
based on the original design proposal.
Please take a look at this specification, and give feedback if it is
unclear or if you anticipate any implementation issues. If there are no
objections, we will schedule the design for implementation in Q4.
@kmillikin <https://github.com/kmillikin> @sigmundch
***@***.***, @mraleph
<https://github.com/mraleph> @vsmenon <https://github.com/vsmenon>
@johnniwinther <https://github.com/johnniwinther> @munificent
<https://github.com/munificent> @danrubel <https://github.com/danrubel>
@jwren <https://github.com/jwren> @jcollins-g
<https://github.com/jcollins-g>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#37 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ANdn2z0cufvFDYXSjezWtv7JDdEn1caZks5urDXmgaJpZM4XMqoc>
.
|
I've fixed the formatting issue and added a "tl;dr" summary section. All set literals would be compile-time errors prior to the feature. |
@lhrn - what valid rewrite transformation we can do in kernel for initial implementation (i.e., without direct backend support)? E.g., the summary suggests this:
Would this also work?:
|
Both should work, or perhaps I'd probably use For constant maps, that won't work. There is currently no constant map implementation in the SDK.
(after removing equal objects from c1..c3), where |
Closing; expected to launch in Dart 2.2 |
Maybe complete effective dart examples |
Yes, will do, tracking in dart-lang/site-www#1365 |
Solution for #36, feature specification.
This issue is for discussing the pros and cons of this particular proposal.
General discussion about the issue should go in #36 where everybody can see it.
I propose to add set literals to Dart by using
<int>{1, 2, 3}
syntax. This is distinguishable from a map literal in every case except the empty set with not type variable,{}
. In that case, we should use the context type to pick the correctSet
orMap
type, defaulting to a map if the context type is not prohibiting it from being a map.(Historical design proposal).
Related issues: dart-lang/sdk#3792
The text was updated successfully, but these errors were encountered: