Skip to content

Commit 8f323d6

Browse files
committed
named tuple toMap extension method
1 parent 98a319a commit 8f323d6

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

library/src/scala/NamedTuple.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package scala
22
import compiletime.ops.boolean.*
3+
import compiletime.summonAll
34

45
import language.experimental.captureChecking
56

@@ -209,6 +210,14 @@ object NamedTupleDecomposition:
209210
/** An immutable array consisting of all element values */
210211
inline def toIArray: IArray[Object] = x.toTuple.toIArray
211212

213+
/** An immutable map consisting of all element values.
214+
* Keys are the names of the elements.
215+
*/
216+
inline def toMap: collection.Map[String, Tuple.Union[V]] =
217+
summonAll[Tuple.Map[N, ValueOf]].toList
218+
.map(_.asInstanceOf[ValueOf[? <: String]].value)
219+
.lazyZip(x.toList)
220+
.toMap
212221
end extension
213222

214223
/** The names of a named tuple, represented as a tuple of literal string values. */

0 commit comments

Comments
 (0)