forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathnamed-tuple-combinators.scala
154 lines (135 loc) · 4.15 KB
/
named-tuple-combinators.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import scala.language.experimental.namedTuples
object Test:
// original code from issue https://github.com/scala/scala3/issues/20427
type NT = NamedTuple.Concat[(hi: Int), (bla: String)]
def foo(x: NT) =
x.hi // error
val y: (hi: Int, bla: String) = x
y.hi // ok
// SELECTOR (reduces to apply)
def foo1(x: NT) =
val res1 = x.hi // error
summon[res1.type <:< Int]
val y: (hi: Int, bla: String) = x
val res2 = y.hi // ok
summon[res2.type <:< Int]
// toTuple
def foo2(x: NT) =
val res1 = x.toTuple
summon[res1.type <:< (Int, String)]
val y: (hi: Int, bla: String) = x
val res2 = y.toTuple
summon[res2.type <:< (Int, String)]
// apply
def foo3(x: NT) =
val res1 = x.apply(1)
summon[res1.type <:< String]
val y: (hi: Int, bla: String) = x
val res2 = y.apply(1)
summon[res2.type <:< String]
// size
def foo4(x: NT) =
class Box:
final val res1 = x.size // final val constrains to a singleton type
summon[res1.type <:< 2]
val y: (hi: Int, bla: String) = x
final val res2 = y.size // final val constrains to a singleton type
summon[res2.type <:< 2]
// head
def foo5(x: NT) =
val res1 = x.head
summon[res1.type <:< Int]
val y: (hi: Int, bla: String) = x
val res2 = y.head
summon[res2.type <:< Int]
// last
def foo6(x: NT) =
val res1 = x.last
summon[res1.type <:< String]
val y: (hi: Int, bla: String) = x
val res2 = y.last
summon[res2.type <:< String]
// init
def foo7(x: NT) =
val res1 = x.init
summon[res1.type <:< (hi: Int)]
val y: (hi: Int, bla: String) = x
val res2 = y.init
summon[res2.type <:< (hi: Int)]
// tail
def foo8(x: NT) =
val res1 = x.tail
summon[res1.type <:< (bla: String)]
val y: (hi: Int, bla: String) = x
val res2 = y.tail
summon[res2.type <:< (bla: String)]
// take
def foo9(x: NT) =
val res1 = x.take(1)
summon[res1.type <:< (hi: Int)]
val y: (hi: Int, bla: String) = x
val res2 = y.take(1)
summon[res2.type <:< (hi: Int)]
// drop
def foo10(x: NT) =
val res1 = x.drop(1)
summon[res1.type <:< (bla: String)]
val y: (hi: Int, bla: String) = x
val res2 = y.drop(1)
summon[res2.type <:< (bla: String)]
// splitAt
def foo11(x: NT) =
val res1 = x.splitAt(1)
summon[res1.type <:< ((hi: Int), (bla: String))]
val y: (hi: Int, bla: String) = x
val res2 = y.splitAt(1)
summon[res2.type <:< ((hi: Int), (bla: String))]
// ++
def foo12(x: NT) =
val res1 = x ++ (baz = 23)
summon[res1.type <:< (hi: Int, bla: String, baz: Int)]
val y: (hi: Int, bla: String) = x
val res2 = y ++ (baz = 23)
summon[res2.type <:< (hi: Int, bla: String, baz: Int)]
// map
def foo13(x: NT) =
val res1 = x.map([T] => (t: T) => Option(t))
summon[res1.type <:< (hi: Option[Int], bla: Option[String])]
val y: (hi: Int, bla: String) = x
val res2 = y.map([T] => (t: T) => Option(t))
summon[res2.type <:< (hi: Option[Int], bla: Option[String])]
// reverse
def foo14(x: NT) =
val res1 = x.reverse
summon[res1.type <:< (bla: String, hi: Int)]
val y: (hi: Int, bla: String) = x
val res2 = y.reverse
summon[res2.type <:< (bla: String, hi: Int)]
// zip
def foo15(x: NT) =
val res1 = x.zip((hi = "xyz", bla = true))
summon[res1.type <:< (hi: (Int, String), bla: (String, Boolean))]
val y: (hi: Int, bla: String) = x
val res2 = y.zip((hi = "xyz", bla = true))
summon[res2.type <:< (hi: (Int, String), bla: (String, Boolean))]
// toList
def foo16(x: NT) =
val res1 = x.toList
summon[res1.type <:< List[Tuple.Union[(Int, String)]]]
val y: (hi: Int, bla: String) = x
val res2 = y.toList
summon[res2.type <:< List[Tuple.Union[(Int, String)]]]
// toArray
def foo17(x: NT) =
val res1 = x.toArray
summon[res1.type <:< Array[Object]]
val y: (hi: Int, bla: String) = x
val res2 = y.toArray
summon[res2.type <:< Array[Object]]
// toIArray
def foo18(x: NT) =
val res1 = x.toIArray
summon[res1.type <:< IArray[Object]]
val y: (hi: Int, bla: String) = x
val res2 = y.toIArray
summon[res2.type <:< IArray[Object]]