From 9f27adbfad9482036cd4d5ebb48570e5ef56542b Mon Sep 17 00:00:00 2001 From: Bob Nystrom Date: Tue, 8 Jan 2019 17:38:05 -0800 Subject: [PATCH] Allow spread element types to disambiguate a set/map literal. --- .../feature-specification.md | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/accepted/future-releases/spread-collections/feature-specification.md b/accepted/future-releases/spread-collections/feature-specification.md index 8a4c1e6ba3..3ba475e8ad 100644 --- a/accepted/future-releases/spread-collections/feature-specification.md +++ b/accepted/future-releases/spread-collections/feature-specification.md @@ -331,18 +331,28 @@ An expression like: ``` Is syntactically parsed as `mapOrSetLiteral`. To determine whether it actually -is a map or set, the surrounding context is used. Given an `mapOrSetLiteral` -with context type C: +is a map or set, the upwards and downwards type inference contexts are used. -* If `Set` is assignable to C, and `Map` is not assignable - to C, then the collection is a set literal. +Given a `mapOrSetLiteral`: -* Otherwise, it is a map literal. +1. If `Map` is assignable to the literal's context type, then it + is a map literal. + +2. Else, if `Set` is assignable to the literal's context type, then the + collection is a set literal. + +3. Else, if it has spread elements and all of the spread element expression + types are subtypes of `Iterable`, then it is a set literal. + + **TODO: Is dynamic a subtype of Iterable? If so, this needs to be tweaked. + If all the spreads are dynamic, then it should be a map literal.** + +4. Otherwise, it is a map literal. In other words, if it can only be a set, it is. Otherwise, it's a map. In cases -where the context type is not specific enough to disambiguate, we could make it -an error instead of defaulting to map. However, that would be inconsistent with -how empty collections are handled. Those have to default to map for backwards +where the types are not specific enough to disambiguate, we could make it an +error instead of defaulting to map. However, that would be inconsistent with how +empty collections are handled. Those have to default to map for backwards compatibility. ### Const spreads