Skip to content

Commit e3e2948

Browse files
Add Expr.ofMap family of methods
1 parent 18578b3 commit e3e2948

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
@@ -114,6 +114,25 @@ object Expr {
114114
def ofList[T](xs: Seq[Expr[T]])(using Type[T], QuoteContext): Expr[List[T]] =
115115
if (xs.isEmpty) Expr(Nil) else '{ List(${Varargs(xs)}: _*) }
116116

117+
/** Lifts the Map the values of which are expressions into an expression of Map. */
118+
def ofMapValues[K: Type: Liftable, V: Type](m: Map[K, Expr[V]])(
119+
using QuoteContext): Expr[Map[K, V]] =
120+
ofMapKeyValues(m.map { case (k, v) => (Expr(k), v) })
121+
122+
/** Lifts the Map the keys of which are expressions into an expression of Map. */
123+
def ofMapKeys[K: Type, V: Type: Liftable](m: Map[Expr[K], V])(
124+
using QuoteContext): Expr[Map[K, V]] =
125+
ofMapKeyValues(m.map { case (k, v) => (k, Expr(v)) })
126+
127+
/** Lifts the Map the keys and values of which are expressions into an expression of Map. */
128+
def ofMapKeyValues[K: Type, V: Type](m: Map[Expr[K], Expr[V]])(
129+
using QuoteContext): Expr[Map[K, V]] =
130+
val listOfExprs: List[Expr[(K, V)]] =
131+
m.iterator.map((k, v) => '{ ($k, $v) }).toList
132+
val exprOfList: Expr[List[(K, V)]] = Expr.ofList(listOfExprs)
133+
'{ Map.from($exprOfList) }
134+
end ofMapKeyValues
135+
117136
/** Lifts this sequence of expressions into an expression of a tuple
118137
*
119138
* 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)