Skip to content

Commit 1f84449

Browse files
Add Expr.ofMap family of methods
1 parent d00a839 commit 1f84449

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

library/src-bootstrapped/scala/quoted/Expr.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,25 @@ object Expr {
130130
def ofList[T](xs: Seq[Expr[T]])(using Type[T], QuoteContext): Expr[List[T]] =
131131
if (xs.isEmpty) '{ Nil } else '{ List(${Varargs(xs)}: _*) }
132132

133+
/** Lifts the Map the values of which are expressions into an expression of Map. */
134+
def ofMapValues[K: Type: Liftable, V: Type](m: Map[K, Expr[V]])(
135+
using QuoteContext): Expr[Map[K, V]] =
136+
ofMapKeyValues(m.map { case (k, v) => (Expr(k), v) })
137+
138+
/** Lifts the Map the keys of which are expressions into an expression of Map. */
139+
def ofMapKeys[K: Type, V: Type: Liftable](m: Map[Expr[K], V])(
140+
using QuoteContext): Expr[Map[K, V]] =
141+
ofMapKeyValues(m.map { case (k, v) => (k, Expr(v)) })
142+
143+
/** Lifts the Map the keys and values of which are expressions into an expression of Map. */
144+
def ofMapKeyValues[K: Type, V: Type](m: Map[Expr[K], Expr[V]])(
145+
using QuoteContext): Expr[Map[K, V]] =
146+
val listOfExprs: List[Expr[(K, V)]] =
147+
m.iterator.map((k, v) => '{ ($k, $v) }).toList
148+
val exprOfList: Expr[List[(K, V)]] = Expr.ofList(listOfExprs)
149+
'{ Map.from($exprOfList) }
150+
end ofMapKeyValues
151+
133152
/** Lifts this sequence of expressions into an expression of a tuple
134153
*
135154
* Transforms a sequence of expression

tests/run-macros/expr-ofMap.check

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
List((a,b), (foo,bar))
2+
List((a,b), (foo,bar))
3+
List((a,b), (foo,bar))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import scala.quoted._
2+
3+
inline def ofMapKeyValues: Map[String, String] = ${ ofMapKeyValuesImpl }
4+
private def ofMapKeyValuesImpl(using QuoteContext): Expr[Map[String, String]] =
5+
Expr.ofMapKeyValues(Map(
6+
'{ "foo" } -> '{ "bar" },
7+
'{ "a" } -> '{ "b" }
8+
))
9+
10+
inline def ofMapKeys: Map[String, String] = ${ ofMapKeysImpl }
11+
private def ofMapKeysImpl(using QuoteContext): Expr[Map[String, String]] =
12+
Expr.ofMapKeys(Map(
13+
'{ "foo" } -> "bar",
14+
'{ "a" } -> "b"
15+
))
16+
17+
inline def ofMapValues: Map[String, String] = ${ ofMapValuesImpl }
18+
private def ofMapValuesImpl(using QuoteContext): Expr[Map[String, String]] =
19+
Expr.ofMapValues(Map(
20+
"foo" -> '{ "bar" },
21+
"a" -> '{ "b" }
22+
))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@main def Test =
2+
println(ofMapKeyValues.toList.sortBy(_._1))
3+
println(ofMapKeys.toList.sortBy(_._1))
4+
println(ofMapValues.toList.sortBy(_._1))

0 commit comments

Comments
 (0)