|
1 | 1 | package scala |
2 | 2 | import compiletime.ops.boolean.* |
| 3 | +import collection.immutable.{SeqMap, ListMap} |
3 | 4 |
|
4 | 5 | import language.experimental.captureChecking |
5 | 6 |
|
@@ -30,7 +31,7 @@ object NamedTuple: |
30 | 31 | import NamedTupleDecomposition.{Names, DropNames} |
31 | 32 | export NamedTupleDecomposition.{ |
32 | 33 | Names, DropNames, |
33 | | - apply, size, init, head, last, tail, take, drop, splitAt, ++, map, reverse, zip, toList, toArray, toIArray |
| 34 | + apply, size, init, head, last, tail, take, drop, splitAt, ++, map, reverse, zip, toList, toArray, toIArray, toSeqMap |
34 | 35 | } |
35 | 36 |
|
36 | 37 | extension [N <: Tuple, V <: Tuple](x: NamedTuple[N, V]) |
@@ -209,6 +210,15 @@ object NamedTupleDecomposition: |
209 | 210 | /** An immutable array consisting of all element values */ |
210 | 211 | inline def toIArray: IArray[Object] = x.toTuple.toIArray |
211 | 212 |
|
| 213 | + /** An immutable map consisting of all element values preserving the order of fields. |
| 214 | + * Keys are the names of the elements. |
| 215 | + */ |
| 216 | + inline def toSeqMap: SeqMap[String, Tuple.Union[V]] = |
| 217 | + inline compiletime.constValueTuple[N].toList match |
| 218 | + case names: List[String] => |
| 219 | + ListMap.from(names.iterator.zip( |
| 220 | + x.toTuple.productIterator.asInstanceOf[Iterator[Tuple.Union[V]]] |
| 221 | + )) |
212 | 222 | end extension |
213 | 223 |
|
214 | 224 | /** The names of a named tuple, represented as a tuple of literal string values. */ |
|
0 commit comments