Skip to content

Commit 9e8f183

Browse files
committed
Fixes to NamedTuple
1 parent ee6d240 commit 9e8f183

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

library/src/scala/NamedTuple.scala

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ object NamedTuple:
4646
inline def head: Tuple.Elem[V, 0] = apply(0)
4747

4848
/** The tuple consisting of all elements of this tuple except the first one */
49-
inline def tail: Tuple.Drop[V, 1] = toTuple.drop(1)
49+
inline def tail: NamedTuple[Tuple.Tail[N], Tuple.Tail[V]] =
50+
toTuple.drop(1).asInstanceOf[NamedTuple[Tuple.Tail[N], Tuple.Tail[V]]]
5051

5152
/** The last element value of this tuple */
5253
inline def last: Tuple.Last[V] = apply(size - 1).asInstanceOf[Tuple.Last[V]]
5354

5455
/** The tuple consisting of all elements of this tuple except the last one */
55-
inline def init: Tuple.Init[V] = toTuple.take(size - 1).asInstanceOf[Tuple.Init[V]]
56+
inline def init: NamedTuple[Tuple.Init[N], Tuple.Init[V]] =
57+
toTuple.take(size - 1).asInstanceOf[NamedTuple[Tuple.Init[N], Tuple.Init[V]]]
5658

5759
/** The tuple consisting of the first `n` elements of this tuple, or all
5860
* elements if `n` exceeds `size`.
@@ -67,7 +69,11 @@ object NamedTuple:
6769
toTuple.drop(n)
6870

6971
/** The tuple `(x.take(n), x.drop(n))` */
70-
inline def splitAt(n: Int): NamedTuple[Tuple.Split[N, n.type], Tuple.Split[V, n.type]] =
72+
inline def splitAt(n: Int):
73+
(NamedTuple[Tuple.Take[N, n.type], Tuple.Take[V, n.type]],
74+
NamedTuple[Tuple.Drop[N, n.type], Tuple.Drop[V, n.type]]) =
75+
// would be nice if this could have type `Split[NamedTuple[N, V]]` instead, but
76+
// we get a type error then. Similar for other methods here.
7177
toTuple.splitAt(n)
7278

7379
/** The tuple consisting of all elements of this tuple followed by all elements
@@ -188,6 +194,12 @@ object NamedTuple:
188194
*/
189195
type From[T] <: AnyNamedTuple
190196

197+
/** The type of the empty named tuple */
198+
type Empty = EmptyTuple.type
199+
200+
/** The empty named tuple */
201+
val Empty: Empty = EmptyTuple.asInstanceOf[Empty]
202+
191203
end NamedTuple
192204

193205
/** Separate from NamedTuple object so that we can match on the opaque type NamedTuple. */
@@ -202,3 +214,4 @@ object NamedTupleDecomposition:
202214
/** The value types of a named tuple represented as a regular tuple. */
203215
type DropNames[NT <: AnyNamedTuple] <: Tuple = NT match
204216
case NamedTuple[_, x] => x
217+

0 commit comments

Comments
 (0)