Skip to content

Commit 5ad07e4

Browse files
committed
More tests
1 parent 9e8f183 commit 5ad07e4

File tree

4 files changed

+92
-8
lines changed

4 files changed

+92
-8
lines changed

tests/neg/named-tuples.check

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,3 @@
101101
| Required: (name : ?, age : ?)
102102
|
103103
| longer explanation available when compiling with `-explain`
104-
-- Warning: tests/neg/named-tuples.scala:27:29 -------------------------------------------------------------------------
105-
27 | val (name = x, agee = y) = person // error
106-
| ^^^^^^
107-
|pattern's type (String, Int) is more specialized than the right hand side expression's type (name : String, age : Int)
108-
|
109-
|If the narrowing is intentional, this can be communicated by adding `: @unchecked` after the expression,
110-
|which may result in a MatchError at runtime.
111-
|This patch can be rewritten automatically under -rewrite -source 3.2-migration.

tests/pos/selectable-fields.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
val foo1 = new Selectable:
2+
type Fields = (xyz: Int)
3+
def selectDynamic(name: String): Any = 23

tests/run/named-tuple-ops.scala

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//> using options -source future
2+
import language.experimental.namedTuples
3+
import scala.compiletime.asMatchable
4+
5+
type City = (name: String, zip: Int, pop: Int)
6+
type Raw = (String, Int, Int)
7+
8+
type Coord = (x: Double, y: Double)
9+
type Labels = (x: String, y: String)
10+
11+
@main def Test =
12+
val city: City = (name = "Lausanne", zip = 1000, pop = 140000)
13+
val coord: Coord = (x = 1.0, y = 0.0)
14+
val labels: Labels = (x = "west", y = "north")
15+
16+
val size: 3 = city.size
17+
assert(city.size == 3)
18+
19+
val zip: Int = city(1)
20+
assert(zip == 1000)
21+
22+
val name: String = city.head
23+
assert(name == "Lausanne")
24+
25+
val zip_pop: (zip: Int, pop: Int) = city.tail
26+
val (_: Int, _: Int) = zip_pop
27+
assert(zip_pop == (zip = 1000, pop = 140000))
28+
29+
val cinit = city.init
30+
val _: (name: String, zip: Int) = cinit
31+
assert(cinit == (name = "Lausanne", zip = 1000))
32+
33+
val ctake1: (name: String) = city.take(1)
34+
assert(ctake1 == (name = "Lausanne"))
35+
36+
val cdrop1 = city.drop(1)
37+
val _: (zip: Int, pop: Int) = cdrop1
38+
assert(cdrop1 == zip_pop)
39+
40+
val cdrop3 = city.drop(3)
41+
val _: NamedTuple.Empty = cdrop3
42+
assert(cdrop3 == NamedTuple.Empty)
43+
44+
val cdrop4 = city.drop(4)
45+
val _: NamedTuple.Empty = cdrop4
46+
assert(cdrop4 == NamedTuple.Empty)
47+
48+
val csplit = city.splitAt(1)
49+
val _: ((name: String), (zip: Int, pop: Int)) = csplit
50+
assert(csplit == ((name = "Lausanne"), zip_pop))
51+
52+
val city_coord = city ++ coord
53+
val _: NamedTuple.Concat[City, Coord] = city_coord
54+
val _: (name: String, zip: Int, pop: Int, x: Double, y: Double) = city_coord
55+
assert(city_coord == (name = "Lausanne", zip = 1000, pop = 140000, x = 1.0, y = 0.0))
56+
57+
type IntToString[X] = X match
58+
case Int => String
59+
case _ => X
60+
61+
val intToString = [X] => (x: X) => x.asMatchable match
62+
case x: Int => x.toString
63+
case x => x
64+
65+
val citymap = city.map[IntToString](intToString.asInstanceOf)
66+
val _: (name: String, zip: String, pop: String) = citymap
67+
assert(citymap == (name = "Lausanne", zip = "1000", pop = "140000"))
68+
69+
val cityreverse = city.reverse
70+
val _: (pop: Int, zip: Int, name: String) = cityreverse
71+
assert(cityreverse == (pop = 140000, zip = 1000, name = "Lausanne"))
72+
73+
val zipped = coord.zip(labels)
74+
val _: (x: (Double, String), y: (Double, String)) = zipped
75+
val (x3, y3) = zipped
76+
val _: (Double, String) = x3
77+
assert(zipped == (x = (1.0, "west"), y = (0.0, "north")))
78+
79+
val zippedRaw = ((1.0, "west"), (0.0, "north"))
80+
val (x1: (Double, String), x2: (Double, String)) = zippedRaw
81+
82+
val cityFields = city.toList
83+
val _: List[String | Int] = cityFields
84+
assert(cityFields == List("Lausanne", 1000, 140000))
85+
86+
val citArr = city.toArray
87+
val _: List[String | Int] = cityFields
88+
assert(cityFields == List("Lausanne", 1000, 140000))
89+

0 commit comments

Comments
 (0)