Skip to content

Commit 25bf111

Browse files
committed
use ListMap instead of HashMap
1 parent d065ef1 commit 25bf111

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

library/src/scala/NamedTuple.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package scala
22
import compiletime.ops.boolean.*
33
import compiletime.summonAll
4+
import collection.immutable.ListMap
45

56
import language.experimental.captureChecking
67

@@ -31,7 +32,7 @@ object NamedTuple:
3132
import NamedTupleDecomposition.{Names, DropNames}
3233
export NamedTupleDecomposition.{
3334
Names, DropNames,
34-
apply, size, init, head, last, tail, take, drop, splitAt, ++, map, reverse, zip, toList, toArray, toIArray, toMap
35+
apply, size, init, head, last, tail, take, drop, splitAt, ++, map, reverse, zip, toList, toArray, toIArray, toListMap
3536
}
3637

3738
extension [N <: Tuple, V <: Tuple](x: NamedTuple[N, V])
@@ -210,12 +211,12 @@ object NamedTupleDecomposition:
210211
/** An immutable array consisting of all element values */
211212
inline def toIArray: IArray[Object] = x.toTuple.toIArray
212213

213-
/** An immutable map consisting of all element values.
214+
/** An immutable map consisting of all element values preserving an order of fields.
214215
* Keys are the names of the elements.
215216
*/
216-
inline def toMap: collection.immutable.Map[String, Tuple.Union[V]] =
217+
inline def toListMap: ListMap[String, Tuple.Union[V]] =
217218
inline compiletime.constValueTuple[N].toList match
218-
case names: List[String] => names.lazyZip(x.toList).toMap
219+
case names: List[String] => ListMap.from(names.zip(x.toList))
219220
end extension
220221

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

tests/run/named-tuple-ops.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//> using options -source future
22
import scala.compiletime.asMatchable
3+
import scala.collection.immutable.ListMap
34

45
type City = (name: String, zip: Int, pop: Int)
56
type Raw = (String, Int, Int)
@@ -86,6 +87,6 @@ type Labels = (x: String, y: String)
8687
val _: Array[Object] = cityArr
8788
assert(cityArr.sameElements(Array("Lausanne", 1000, 140000)))
8889

89-
val cityMap = city.toMap
90-
val _: Map[String, String | Int] = cityMap
91-
assert(cityMap == Map("name" -> "Lausanne", "zip" -> 1000, "pop" -> 140000))
90+
val cityMap = city.toListMap
91+
val _: ListMap[String, String | Int] = cityMap
92+
assert(cityMap == ListMap("name" -> "Lausanne", "zip" -> 1000, "pop" -> 140000))

0 commit comments

Comments
 (0)