-
Notifications
You must be signed in to change notification settings - Fork 31
WIP: proposition for one-element tuple syntax #121
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
Conversation
- Clarify grammar rules for each tuple case (empty, single, multi-element) - Add section on interaction with trailing comma tolerance (SIP-27) - Document multiline tuple parsing behavior - Update proof of concept section with both implementation variants
…t in different variants and more text cleanup
e17f377 to
7bafabd
Compare
|
|
||
| This proposal introduces trailing comma syntax for tuples, allowing `(A,)` to represent a single-element tuple type and `(a,)` to represent a single-element tuple value. This addresses the long-standing ambiguity where `(A)` is parsed as a parenthesized expression rather than a tuple, making it impossible to express single-element tuples using the concise parenthesized notation. | ||
|
|
||
| The proposal also allows `(,)` for empty tuples (equivalent to `EmptyTuple`) and trailing commas in multi-element tuples like `(A, B,)`, providing consistency with other Scala constructs that already support trailing commas. |
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.
As far as I know, no Scala construct supports trailing commas without a newline following them.
That also means that (a,) is already valid syntax, if you have a newline after the value:
scala> (
| 1,
| )
val res0: Int = 1Seems a bit sketchy to have (1,) and (1,\n) mean different things. I personally find it very common to find (a, b) tuple literals growing large enough that I spread them over multiple lines, and I expect it would be common for single-element tuples as well. With this proposal, we would see a behavior change when a tuple like (a,) grew big enough that the opening and closing parens get moved to their own line
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.
In Python (1,\n) is indeed a tuple, and distinct from (1\n), so there is precedence for such a fine distinction.
>>> (
... 1,
... )
(1,)
>>> (
... 1
... )
1But in Scala (1,\n) cannot be made into a tuple without breaking backwards compatibility.
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.
Oops, then the maximum we can do is to fix the Tuple arity.
|
We already have scala> Tuple(1)
val res1: Int *: EmptyTuple = (1,)
scala> Tuple()
val res2: EmptyTuple.type = () The Maybe we can standardize on saying Furthermore, an Notably, scala> Tuple(1, 2, 3)
-- [E134] Type Error: ----------------------------------------------------------
1 |Tuple(1, 2, 3)
|^^^^^
|None of the overloaded alternatives of method apply in object Tuple with types
| [T](x: T): T *: EmptyTuple
| (): EmptyTuple
|match arguments ((1 : Int), (2 : Int), (3 : Int))
|
|where: T is a type variable
1 error foundPerhaps that can be fixed so that |
|
I think generalizing |
|
yes, looks like (T,) idea is dead because of backward compability. |
|
Or we can save this for scala4. (i.e. resubmit when start collecting information about non-backward compatible changes). |
No, library changes don't need a SIP. |
|
library changes submitted: scala/scala3#24874 |
No description provided.