Skip to content

Commit bd9bb8a

Browse files
committed
Various tweaks
1 parent 5dd48f9 commit bd9bb8a

File tree

7 files changed

+55
-39
lines changed

7 files changed

+55
-39
lines changed

docs/_docs/reference/experimental/named-tuples.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ This follows since a regular tuple `(T_1, ..., T_n)` is treated as a subtype of
3838
```scala
3939
val x: (String, Int) = Bob // error: type mismatch
4040
```
41-
One can convert a named tuple to an unnamed tuple with the `dropNames` method, so the following works:
41+
One can convert a named tuple to an unnamed tuple with the `toTuple` method, so the following works:
4242
```scala
43-
val x: (String, Int) = Bob.dropNames // ok
43+
val x: (String, Int) = Bob.toTuple // ok
4444
```
4545
Note that conformance rules for named tuples are analogous to the rules for named parameters. One can assign parameters by position to a named parameter list.
4646
```scala

library/src/scala/NamedTuple.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ object NamedTuple:
88
opaque type AnyNamedTuple = Any
99
opaque type NamedTuple[N <: Tuple, +V <: Tuple] >: V <: AnyNamedTuple = V
1010

11-
def apply[N <: Tuple, V <: Tuple](x: V) = x
11+
def apply[N <: Tuple, V <: Tuple](x: V): NamedTuple[N, V] = x
1212

1313
def unapply[N <: Tuple, V <: Tuple](x: NamedTuple[N, V]): Some[V] = Some(x)
1414

library/src/scala/Tuple.scala

-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ object Tuple:
354354
/** A boolean indicating whether there is an element in the type `X` of `x`
355355
* that matches type `Y`.
356356
*/
357-
358357
inline def containsType[Y] = constValue[Contains[X, Y]]
359358

360359
/* Note: It would be nice to add the following two extension methods:

tests/neg/named-tuples.check

+39-32
Original file line numberDiff line numberDiff line change
@@ -21,80 +21,87 @@
2121
| Required: (String, Int)
2222
|
2323
| longer explanation available when compiling with `-explain`
24-
-- [E007] Type Mismatch Error: tests/neg/named-tuples.scala:19:20 ------------------------------------------------------
25-
19 | val _: NameOnly = person // error
24+
-- [E007] Type Mismatch Error: tests/neg/named-tuples.scala:19:25 ------------------------------------------------------
25+
19 | val _: (String, Int) = (name = "", age = 0) // error
26+
| ^^^^^^^^^^^^^^^^^^^^
27+
| Found: (name : String, age : Int)
28+
| Required: (String, Int)
29+
|
30+
| longer explanation available when compiling with `-explain`
31+
-- [E007] Type Mismatch Error: tests/neg/named-tuples.scala:20:20 ------------------------------------------------------
32+
20 | val _: NameOnly = person // error
2633
| ^^^^^^
2734
| Found: (Test.person : (name : String, age : Int))
2835
| Required: Test.NameOnly
2936
|
3037
| longer explanation available when compiling with `-explain`
31-
-- [E007] Type Mismatch Error: tests/neg/named-tuples.scala:20:18 ------------------------------------------------------
32-
20 | val _: Person = nameOnly // error
38+
-- [E007] Type Mismatch Error: tests/neg/named-tuples.scala:21:18 ------------------------------------------------------
39+
21 | val _: Person = nameOnly // error
3340
| ^^^^^^^^
3441
| Found: (Test.nameOnly : (name : String))
3542
| Required: Test.Person
3643
|
3744
| longer explanation available when compiling with `-explain`
38-
-- [E007] Type Mismatch Error: tests/neg/named-tuples.scala:22:36 ------------------------------------------------------
39-
22 | val _: (age: Int, name: String) = person // error
45+
-- [E007] Type Mismatch Error: tests/neg/named-tuples.scala:23:36 ------------------------------------------------------
46+
23 | val _: (age: Int, name: String) = person // error
4047
| ^^^^^^
4148
| Found: (Test.person : (name : String, age : Int))
4249
| Required: (age : Int, name : String)
4350
|
4451
| longer explanation available when compiling with `-explain`
45-
-- Error: tests/neg/named-tuples.scala:24:17 ---------------------------------------------------------------------------
46-
24 | val (name = x, agee = y) = person // error
52+
-- Error: tests/neg/named-tuples.scala:25:17 ---------------------------------------------------------------------------
53+
25 | val (name = x, agee = y) = person // error
4754
| ^^^^^^^^
4855
| No element named `agee` is defined in selector type (name : String, age : Int)
49-
-- Error: tests/neg/named-tuples.scala:27:10 ---------------------------------------------------------------------------
50-
27 | case (name = n, age = a) => () // error // error
56+
-- Error: tests/neg/named-tuples.scala:28:10 ---------------------------------------------------------------------------
57+
28 | case (name = n, age = a) => () // error // error
5158
| ^^^^^^^^
5259
| No element named `name` is defined in selector type (String, Int)
53-
-- Error: tests/neg/named-tuples.scala:27:20 ---------------------------------------------------------------------------
54-
27 | case (name = n, age = a) => () // error // error
60+
-- Error: tests/neg/named-tuples.scala:28:20 ---------------------------------------------------------------------------
61+
28 | case (name = n, age = a) => () // error // error
5562
| ^^^^^^^
5663
| No element named `age` is defined in selector type (String, Int)
57-
-- [E172] Type Error: tests/neg/named-tuples.scala:29:27 ---------------------------------------------------------------
58-
29 | val pp = person ++ (1, 2) // error
64+
-- [E172] Type Error: tests/neg/named-tuples.scala:30:27 ---------------------------------------------------------------
65+
30 | val pp = person ++ (1, 2) // error
5966
| ^
6067
| Cannot prove that Tuple.Disjoint[(("name" : String), ("age" : String)), Tuple] =:= (true : Boolean).
61-
-- [E172] Type Error: tests/neg/named-tuples.scala:32:18 ---------------------------------------------------------------
62-
32 | person ++ (1, 2) match // error
68+
-- [E172] Type Error: tests/neg/named-tuples.scala:33:18 ---------------------------------------------------------------
69+
33 | person ++ (1, 2) match // error
6370
| ^
6471
| Cannot prove that Tuple.Disjoint[(("name" : String), ("age" : String)), Tuple] =:= (true : Boolean).
65-
-- Error: tests/neg/named-tuples.scala:35:17 ---------------------------------------------------------------------------
66-
35 | val bad = ("", age = 10) // error
72+
-- Error: tests/neg/named-tuples.scala:36:17 ---------------------------------------------------------------------------
73+
36 | val bad = ("", age = 10) // error
6774
| ^^^^^^^^
6875
| Illegal combination of named and unnamed tuple elements
69-
-- Error: tests/neg/named-tuples.scala:38:20 ---------------------------------------------------------------------------
70-
38 | case (name = n, age) => () // error
76+
-- Error: tests/neg/named-tuples.scala:39:20 ---------------------------------------------------------------------------
77+
39 | case (name = n, age) => () // error
7178
| ^^^
7279
| Illegal combination of named and unnamed tuple elements
73-
-- Error: tests/neg/named-tuples.scala:39:16 ---------------------------------------------------------------------------
74-
39 | case (name, age = a) => () // error
80+
-- Error: tests/neg/named-tuples.scala:40:16 ---------------------------------------------------------------------------
81+
40 | case (name, age = a) => () // error
7582
| ^^^^^^^
7683
| Illegal combination of named and unnamed tuple elements
77-
-- Error: tests/neg/named-tuples.scala:42:10 ---------------------------------------------------------------------------
78-
42 | case (age = x) => // error
84+
-- Error: tests/neg/named-tuples.scala:43:10 ---------------------------------------------------------------------------
85+
43 | case (age = x) => // error
7986
| ^^^^^^^
8087
| No element named `age` is defined in selector type Tuple
81-
-- [E172] Type Error: tests/neg/named-tuples.scala:44:27 ---------------------------------------------------------------
82-
44 | val p2 = person ++ person // error
88+
-- [E172] Type Error: tests/neg/named-tuples.scala:45:27 ---------------------------------------------------------------
89+
45 | val p2 = person ++ person // error
8390
| ^
8491
|Cannot prove that Tuple.Disjoint[(("name" : String), ("age" : String)), (("name" : String), ("age" : String))] =:= (true : Boolean).
85-
-- [E172] Type Error: tests/neg/named-tuples.scala:45:43 ---------------------------------------------------------------
86-
45 | val p3 = person ++ (first = 11, age = 33) // error
92+
-- [E172] Type Error: tests/neg/named-tuples.scala:46:43 ---------------------------------------------------------------
93+
46 | val p3 = person ++ (first = 11, age = 33) // error
8794
| ^
8895
|Cannot prove that Tuple.Disjoint[(("name" : String), ("age" : String)), (("first" : String), ("age" : String))] =:= (true : Boolean).
89-
-- [E007] Type Mismatch Error: tests/neg/named-tuples.scala:47:22 ------------------------------------------------------
90-
47 | val p5 = person.zip(first = 11, age = 33) // error
96+
-- [E007] Type Mismatch Error: tests/neg/named-tuples.scala:48:22 ------------------------------------------------------
97+
48 | val p5 = person.zip(first = 11, age = 33) // error
9198
| ^^^^^^^^^^^^^^^^^^^^
9299
| Found: (first : Int, age : Int)
93100
| Required: NamedTuple.NamedTuple[(("name" : String), ("age" : String)), Tuple]
94101
|
95102
| longer explanation available when compiling with `-explain`
96-
-- Warning: tests/neg/named-tuples.scala:24:29 -------------------------------------------------------------------------
97-
24 | val (name = x, agee = y) = person // error
103+
-- Warning: tests/neg/named-tuples.scala:25:29 -------------------------------------------------------------------------
104+
25 | val (name = x, agee = y) = person // error
98105
| ^^^^^^
99106
|pattern's type (String, Int) is more specialized than the right hand side expression's type (name : String, age : Int)
100107
|

tests/neg/named-tuples.scala

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import language.experimental.namedTuples
1616
val nameOnly = (name = "Louis")
1717

1818
val y: (String, Int) = person // error
19+
val _: (String, Int) = (name = "", age = 0) // error
1920
val _: NameOnly = person // error
2021
val _: Person = nameOnly // error
2122

tests/run/named-tuples.check

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ no match
77
Bob is younger than Bill
88
Bob is younger than Lucy
99
Bill is younger than Lucy
10+
(((Lausanne,Pully),Preverenges),((1003,1009),1028))

tests/run/named-tuples.scala

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import language.experimental.namedTuples
2-
import NamedTuple.toTuple
2+
import NamedTuple.*
33

44
type Person = (name: String, age: Int)
55
val bob = (name = "Bob", age = 33): (name: String, age: Int)
@@ -9,8 +9,8 @@ type Uni = (uni: Double)
99
val uni = (uni = 1.0)
1010
val _: Uni = uni
1111

12-
type AddressInfo = (city: String, zip: Int)
13-
val addr = (city = "Lausanne", zip = 1003)
12+
type AddressInfo = (city: String, zipCode: Int)
13+
val addr = (city = "Lausanne", zipCode = 1003)
1414
val _: AddressInfo = addr
1515

1616
type CombinedInfo = NamedTuple.Concat[Person, AddressInfo]
@@ -95,5 +95,13 @@ val _: CombinedInfo = bob ++ addr
9595
val x: Person = bob1 // bob1 still has type Person with the unswapped elements
9696
case _ => assert(false)
9797

98+
val addr2 = (city = "Pully", zipCode = 1009)
99+
val addr3 = addr.zip(addr2)
100+
val addr4 = addr3.zip("Preverenges", 1028)
101+
println(addr4)
102+
103+
104+
105+
98106

99107

0 commit comments

Comments
 (0)