-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
A strawman for aggregate literals #21993
Draft
odersky
wants to merge
14
commits into
scala:main
Choose a base branch
from
dotty-staging:try-seqliterals
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+510
−27
Draft
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
cb27cb5
A strawman for aggregate literals
odersky cd4ecf5
Prototype InlineConversion as well.
odersky 7138b8d
Add assert to typedSeqLiteral
odersky 8252149
Implement collection literals
odersky 93a1a78
Require a language import for enabling collection literals
odersky 91a5be5
Use a different scheme for deciding when to use the default
odersky ffac28b
More tests
odersky 52392c7
Add a doc page
odersky 6e904fb
Add Mimafilters
odersky 89e80b9
Add tests of literals with elements of mmixed types
odersky 599d51c
Fix syntax.md typo
odersky 83e0372
Polishing
odersky ceb08a7
Avoid repl crash and document that we need 3.7
odersky 28700de
Update check file
odersky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
--- | ||
layout: doc-page | ||
title: "Collection Literals" | ||
redirectFrom: /docs/reference/other-new-features/collection-literals.html | ||
nightlyOf: https://docs.scala-lang.org/scala3/reference/experimental/collection-literals.html | ||
--- | ||
|
||
|
||
Support for collection literals is enabled by the experimental language import | ||
```scala | ||
import scala.language.experimental.collectionLiterals | ||
``` | ||
Collection literals are comma-separated sequences of expressions, like these: | ||
```scala | ||
val oneTwoThree = [1, 2, 3] | ||
val anotherLit = [math.Pi, math.cos(2.0), math.E * 3.0] | ||
val diag = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] | ||
val empty = [] | ||
val mapy = [1 -> "one", 2 -> "two", 3 -> "three"] | ||
``` | ||
The type of a collection literal depends on the expected type. If there is no expected type (as in the examples above) a collection literal is of type `Seq`, except if it consists exclusively elements of the form `a -> b`, then it is of type `Map`. For instance, the literals above would | ||
get inferred types as follows. | ||
```scala | ||
val oneTwoThree: Seq[Int] = [1, 2, 3] | ||
val anotherLit: Seq[Double] = [math.Pi, math.cos(2.0), math.E * 3.0] | ||
val diag: Seq[Seq[Int]] = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] | ||
val empty: Seq[Nothing] = [] | ||
val mapy: Map[Int, String] = [1 -> "one", 2 -> "two", 3 -> "three"] | ||
``` | ||
If there is an expected type `E`, the compiler will search for a given instance of the | ||
type class `ExpressibleAsCollectionLiteral[E]`. This type class is defined in package `scala.compiletime` as follows: | ||
```scala | ||
trait ExpressibleAsCollectionLiteral[+Coll]: | ||
|
||
/** The element type of the created collection */ | ||
type Elem | ||
|
||
/** The inline method that creates the collection */ | ||
inline def fromLiteral(inline xs: Elem*): Coll | ||
``` | ||
If a best matching instance `ecl` is found, its `fromLiteral` method is used to convert | ||
the elements of the literal to the expected type. If the search is ambiguous, it will be | ||
reported as an error. If no matching instance is found, the literal will be typed by the default scheme as if there was no expected type. | ||
|
||
The companion object of `ExpressibleAsCollectionLiteral` contains a number of given instances for standard collection types. For instance, there is: | ||
```scala | ||
given vectorFromLiteral: [T] => ExpressibleAsCollectionLiteral[Vector[T]]: | ||
type Elem = T | ||
inline def fromLiteral(inline xs: T*) = Vector[Elem](xs*) | ||
``` | ||
Hence, the definition | ||
```scala | ||
val v: Vector[Int] = [1, 2, 3] | ||
``` | ||
would be expanded by the compiler to | ||
```scala | ||
val v: Vector[Int] = vectorFromLiteral.fromLiteral(1, 2, 3) | ||
``` | ||
After inlining, this produces | ||
```scala | ||
val v: Vector[Int] = Vector[Int](1, 2, 3) | ||
``` | ||
Using this scheme, the literals we have seen earlier could also be given alternative types like these: | ||
```scala | ||
val oneTwoThree: Vector[Int] = [1, 2, 3] | ||
val anotherLit: IArray[Double] = [math.Pi, math.cos(2.0), math.E * 3.0] | ||
val diag: Array[Array[Int]] = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] | ||
val empty: ArrayBuffer[Object] = [] | ||
val mapy: HashMap[Int, String] = [1 -> "one", 2 -> "two", 3 -> "three"] | ||
``` | ||
|
||
**Notes** | ||
|
||
- Since the fromLiteral method in `ExpressibleAsCollectionLiteral` is an inline method with inline arguments, given instances can implement it as a macro. | ||
|
||
- The precise meaning of "is there an expected type?" is as follows: There is no expected | ||
type if the expected type known from the context is _underdefined_ as specified for | ||
implicit search. That is, implicit search for a conversion to the expected type would fail with an error message that contains a note like this: | ||
``` | ||
Note that implicit conversions were not tried because the result of an implicit conversion|must be more specific than ... | ||
``` | ||
Concretely, this is the case for Wildcard types `?`, `Any`, `AnyRef`, or type variables | ||
bounded by one of these types. | ||
|
||
**Syntax** | ||
|
||
``` | ||
SimpleExpr ::= ... | ||
| ‘[’ ExprInParens {‘,’ ExprInParens} ‘]’ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just noticed while having a look