Skip to content

Commit f931a6d

Browse files
Use the NamedTuple type ops for the result types of the term ops
1 parent 0a416d8 commit f931a6d

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

library/src-bootstrapped/scala/NamedTuple.scala

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ object NamedTuple:
2525
extension [V <: Tuple](x: V)
2626
inline def withNames[N <: Tuple]: NamedTuple[N, V] = x
2727

28+
import NamedTupleDecomposition.{Names, DropNames}
2829
export NamedTupleDecomposition.{
2930
Names, DropNames,
3031
apply, size, init, head, last, tail, take, drop, splitAt, ++, map, reverse, zip, toList, toArray, toIArray
@@ -134,65 +135,57 @@ object NamedTupleDecomposition:
134135
import NamedTuple.*
135136
extension [N <: Tuple, V <: Tuple](x: NamedTuple[N, V])
136137
/** The value (without the name) at index `n` of this tuple */
137-
inline def apply(n: Int): Tuple.Elem[V, n.type] =
138+
inline def apply(n: Int): Elem[NamedTuple[N, V], n.type] =
138139
inline x.toTuple match
139-
case tup: NonEmptyTuple => tup(n).asInstanceOf[Tuple.Elem[V, n.type]]
140-
case tup => tup.productElement(n).asInstanceOf[Tuple.Elem[V, n.type]]
140+
case tup: NonEmptyTuple => tup(n).asInstanceOf[Elem[NamedTuple[N, V], n.type]]
141+
case tup => tup.productElement(n).asInstanceOf[Elem[NamedTuple[N, V], n.type]]
141142

142143
/** The number of elements in this tuple */
143-
inline def size: Tuple.Size[V] = x.toTuple.size
144+
inline def size: Size[NamedTuple[N, V]] = x.toTuple.size
144145

145146
/** The first element value of this tuple */
146-
inline def head: Tuple.Elem[V, 0] = apply(0)
147+
inline def head: Head[NamedTuple[N, V]] = apply(0)
147148

148149
/** The last element value of this tuple */
149-
inline def last: Tuple.Last[V] = apply(size - 1).asInstanceOf[Tuple.Last[V]]
150+
inline def last: Last[NamedTuple[N, V]] = apply(size - 1).asInstanceOf[Tuple.Last[V]]
150151

151152
/** The tuple consisting of all elements of this tuple except the last one */
152-
inline def init: NamedTuple[Tuple.Init[N], Tuple.Init[V]] =
153-
x.toTuple.take(size - 1).asInstanceOf[NamedTuple[Tuple.Init[N], Tuple.Init[V]]]
153+
inline def init: Init[NamedTuple[N, V]] =
154+
x.toTuple.take(size - 1).asInstanceOf[Init[NamedTuple[N, V]]]
154155

155156
/** The tuple consisting of all elements of this tuple except the first one */
156-
inline def tail: NamedTuple[Tuple.Tail[N], Tuple.Tail[V]] =
157-
x.toTuple.drop(1).asInstanceOf[NamedTuple[Tuple.Tail[N], Tuple.Tail[V]]]
157+
inline def tail: Tail[NamedTuple[N, V]] =
158+
x.toTuple.drop(1).asInstanceOf[Tail[NamedTuple[N, V]]]
158159

159160
/** The tuple consisting of the first `n` elements of this tuple, or all
160161
* elements if `n` exceeds `size`.
161162
*/
162-
inline def take(n: Int): NamedTuple[Tuple.Take[N, n.type], Tuple.Take[V, n.type]] =
163-
x.toTuple.take(n)
163+
inline def take(n: Int): Take[NamedTuple[N, V], n.type] = x.toTuple.take(n)
164164

165165
/** The tuple consisting of all elements of this tuple except the first `n` ones,
166166
* or no elements if `n` exceeds `size`.
167167
*/
168-
inline def drop(n: Int): NamedTuple[Tuple.Drop[N, n.type], Tuple.Drop[V, n.type]] =
169-
x.toTuple.drop(n)
168+
inline def drop(n: Int): Drop[NamedTuple[N, V], n.type] = x.toTuple.drop(n)
170169

171170
/** The tuple `(x.take(n), x.drop(n))` */
172-
inline def splitAt(n: Int):
173-
(NamedTuple[Tuple.Take[N, n.type], Tuple.Take[V, n.type]],
174-
NamedTuple[Tuple.Drop[N, n.type], Tuple.Drop[V, n.type]]) =
175-
// would be nice if this could have type `Split[NamedTuple[N, V]]` instead, but
176-
// we get a type error then. Similar for other methods here.
177-
x.toTuple.splitAt(n)
171+
inline def splitAt(n: Int): Split[NamedTuple[N, V], n.type] = x.toTuple.splitAt(n)
178172

179173
/** The tuple consisting of all elements of this tuple followed by all elements
180174
* of tuple `that`. The names of the two tuples must be disjoint.
181175
*/
182176
inline def ++ [N2 <: Tuple, V2 <: Tuple](that: NamedTuple[N2, V2])(using Tuple.Disjoint[N, N2] =:= true)
183-
: NamedTuple[Tuple.Concat[N, N2], Tuple.Concat[V, V2]]
177+
: Concat[NamedTuple[N, V], NamedTuple[N2, V2]]
184178
= x.toTuple ++ that.toTuple
185179

186180
/** The named tuple consisting of all element values of this tuple mapped by
187181
* the polymorphic mapping function `f`. The names of elements are preserved.
188182
* If `x = (n1 = v1, ..., ni = vi)` then `x.map(f) = `(n1 = f(v1), ..., ni = f(vi))`.
189183
*/
190-
inline def map[F[_]](f: [t] => t => F[t]): NamedTuple[N, Tuple.Map[V, F]] =
184+
inline def map[F[_]](f: [t] => t => F[t]): Map[NamedTuple[N, V], F] =
191185
x.toTuple.map(f).asInstanceOf[NamedTuple[N, Tuple.Map[V, F]]]
192186

193187
/** The named tuple consisting of all elements of this tuple in reverse */
194-
inline def reverse: NamedTuple[Tuple.Reverse[N], Tuple.Reverse[V]] =
195-
x.toTuple.reverse
188+
inline def reverse: Reverse[NamedTuple[N, V]] = x.toTuple.reverse
196189

197190
/** The named tuple consisting of all elements values of this tuple zipped
198191
* with corresponding element values in named tuple `that`.
@@ -201,7 +194,7 @@ object NamedTupleDecomposition:
201194
* The names of `x` and `that` at the same index must be the same.
202195
* The result tuple keeps the same names as the operand tuples.
203196
*/
204-
inline def zip[V2 <: Tuple](that: NamedTuple[N, V2]): NamedTuple[N, Tuple.Zip[V, V2]] =
197+
inline def zip[V2 <: Tuple](that: NamedTuple[N, V2]): Zip[NamedTuple[N, V], NamedTuple[N, V2]] =
205198
x.toTuple.zip(that.toTuple)
206199

207200
/** A list consisting of all element values */

0 commit comments

Comments
 (0)