-
Notifications
You must be signed in to change notification settings - Fork 28
SIP-70 - Flexible Varargs #105
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
base: main
Are you sure you want to change the base?
Conversation
content/flexible-varargs.md
Outdated
``` | ||
|
||
### Javascript | ||
Javascript's expression `...` syntax works identically to this proposal. In Python, you can mix |
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.
Python should be Javascript
On the pattern side, the old gotcha is scala/bug#7623 (which has a lint on Scala 2). That is where two pat vars are satisfied by a single Seq, i.e., there is a kind of misalignment. (The example may temper ambition for patterns.) |
I started to wander if this SIP could help resolve this issue: scala/scala3#18009 mySelectable.foo(args1*)(args2*) should get desugared to something like mySelectable.applyDynamic("foo")(args1*, args2*).asInstanceOf[Foo] which is now illegal, but would be when this SIP gets implemented |
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.
What's proposed:
- Constructing varargs with multiple splices xs*, interspersed with normal values
- Deconstructing sequences with a single vararg pattern that can be somewhere in the middle of the sequence, not just at the end.
These are clear quality of life improvements. The construction part is the more important one, and it should be straightforward to implement. The pattern matching part is probably also easy to implement, but it might need some care to get good performance for linear sequences. E.g you have a long list and match with List(x, y, ys*, 2, 3)
you want to avoid multiple trips to the end of the list to match the 2
and 3
. Nevertheless, it looks quite doable.
cleanup using `+:` and `:+` doesn't actually work due to weird associativity problems. | ||
With this proposal, you can write what you mean and have it just work. | ||
|
||
### Constucting Sequences |
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.
### Constucting Sequences | |
### Constructing Sequences |
|
||
### Constucting Sequences | ||
|
||
The second scenarios that this streamlines is constructing `Seq`s and other collections. |
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.
The second scenarios that this streamlines is constructing `Seq`s and other collections. | |
The second scenario that this streamlines is constructing `Seq`s and other collections. |
val coll = Seq() ++ foo ++ bar ++ Seq(qux, baz) | ||
``` | ||
|
||
With those proposal, all three scenarios would look almost the same - reflecting |
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.
With those proposal, all three scenarios would look almost the same - reflecting | |
With this proposal, all three scenarios would look almost the same - reflecting |
I also think this SIP is a very nice improvement. You mention / suggest to use
I'm not sure if the scala> collection.immutable.ArraySeq.newBuilder.addOne(1).result()
-- [E172] Type Error: ----------------------------------------------------------
1 |collection.immutable.ArraySeq.newBuilder.addOne(1).result()
| ^
| No ClassTag available for Any
1 error found Interestingly, the same line works on Scala 2, the compiler infers If an explicit type argument is needed, the SIP should specify how that type is obtained. For patterns, we'll need to update the spec to say how This SIP doesn't change repeated parameters (of case classes), a repeated parameter will still be last in a case classs definition. An So the spec needs to map the new pattern shape onto to current case class parameter list shape (or unapplySeq return type shape). Note that |
No description provided.