Skip to content

Commit

Permalink
Move NamedTuple.head to NamedTupleDecomposition
Browse files Browse the repository at this point in the history
This is in particular necessary for scala#21291,
to avoid problems encountered after inlining from scopes defining opaque types
(such as in the example below),
as was already done for the other NamedTuple operations in scala#20504.

```scala
-- Error: tests/pos/named-tuple-combinators.scala:46:17 ------------------------
46 |    val res1 = x.head
   |               ^^^^^^
   |(Int, String) does not conform to bound >:
   |  (x$proxy55 : (x : Test.NT) &
   |    $proxy19.NamedTuple[
   |      Tuple.Concat[
   |        NamedTupleDecomposition.Names[
   |          $proxy19.NamedTuple[Tuple1[("hi" : String)], Tuple1[Int]]],
   |        NamedTupleDecomposition.Names[
   |          $proxy19.NamedTuple[Tuple1[("bla" : String)], Tuple1[String]]]
   |      ],
   |      Tuple.Concat[
   |        NamedTupleDecomposition.DropNames[
   |          $proxy19.NamedTuple[Tuple1[("hi" : String)], Tuple1[Int]]],
   |        NamedTupleDecomposition.DropNames[
   |          $proxy19.NamedTuple[Tuple1[("bla" : String)], Tuple1[String]]]
   |      ]
   |    ]
   |  )
   | <: Tuple
   |----------------------------------------------------------------------------
   |Inline stack trace
   |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   |This location contains code that was inlined from NamedTuple.scala:47
47 |    inline def head: Tuple.Elem[V, 0] = x.apply(0)
   |                                        ^^^^^^^
    ----------------------------------------------------------------------------
```
  • Loading branch information
EugeneFlesselle committed Jul 31, 2024
1 parent 3dfd762 commit 32e4056
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions library/src/scala/NamedTuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object NamedTuple:

export NamedTupleDecomposition.{
Names, DropNames,
apply, size, init, last, tail, take, drop, splitAt, ++, map, reverse, zip, toList, toArray, toIArray
apply, size, init, head, last, tail, take, drop, splitAt, ++, map, reverse, zip, toList, toArray, toIArray
}

extension [N <: Tuple, V <: Tuple](x: NamedTuple[N, V])
Expand All @@ -43,9 +43,6 @@ object NamedTuple:
// and should be reverted, just like NonEmptyList is also appealing at first, but a bad idea
// in the end.

/** The first element value of this tuple */
inline def head: Tuple.Elem[V, 0] = x.apply(0)

// inline def :* [L] (x: L): NamedTuple[Append[N, ???], Append[V, L] = ???
// inline def *: [H] (x: H): NamedTuple[??? *: N], H *: V] = ???

Expand Down Expand Up @@ -149,6 +146,9 @@ object NamedTupleDecomposition:
/** The number of elements in this tuple */
inline def size: Tuple.Size[V] = x.toTuple.size

/** The first element value of this tuple */
inline def head: Tuple.Elem[V, 0] = apply(0)

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

Expand Down

0 comments on commit 32e4056

Please sign in to comment.