Skip to content

Commit 262970a

Browse files
committed
Add second compatibility problem suggested by Li Haoyi
1 parent 504836e commit 262970a

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

content/named-tuples.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,31 @@ Named tuple types and expressions are simply desugared to types and trees alread
226226
227227
Pattern matching with named fields requires some small additions to Typer and the PatternMatcher phase. It does not change the Tasty format, though.
228228
229-
Backward source compatibility is partially preserved since additions to types and patterns come with new syntax that was not expressible before. When looking at tuple expressions, we have one instance of a source incompatibility:
229+
Backward source compatibility is partially preserved since additions to types and patterns come with new syntax that was not expressible before. When looking at tuple expressions, we have two instances of a source incompatibility:
230230
231231
```scala
232232
var age: Int
233233
(age = 1)
234234
```
235-
This was an assignment in parentheses before, and is a named tuple of arity one now. It is however not idiomatic Scala code, since assignments are not usually enclosed in parentheses. The problem could also be detected and diagnosed fairly straightforwardly: When faced with a unary named tuple, try to interpret it as an assignment, and if that succeeds, issue a migration error.
235+
This was an assignment in parentheses before, and is a named tuple of arity one now. It is however not idiomatic Scala code, since assignments are not usually enclosed in parentheses.
236+
237+
Also, if we have
238+
```scala
239+
class C:
240+
infix def f(age: Int)
241+
val c: C
242+
```
243+
then
244+
```scala
245+
c f (age = 1)
246+
```
247+
will now construct a tuple as second operand instead of passing a named parameter.
248+
249+
These problems can be detected and diagnosed fairly straightforwardly: When faced with a unary named tuple, try to interpret it as an assignment, and if that succeeds, issue a migration error and suggest a workaround of these kinds:
250+
```scala
251+
{age = 1} // ok
252+
c.f(age = 1) // ok
253+
```
236254

237255
### Open questions
238256

0 commit comments

Comments
 (0)