Skip to content

Add Expr.ofMap family of methods #9278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions library/src-bootstrapped/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,25 @@ object Expr {
def ofList[T](xs: Seq[Expr[T]])(using Type[T], QuoteContext): Expr[List[T]] =
if (xs.isEmpty) Expr(Nil) else '{ List(${Varargs(xs)}: _*) }

/** Lifts the Map the values of which are expressions into an expression of Map. */
def ofMapValues[K: Type: Liftable, V: Type](m: Map[K, Expr[V]])(
using QuoteContext): Expr[Map[K, V]] =
ofMapKeyValues(m.map { case (k, v) => (Expr(k), v) })

/** Lifts the Map the keys of which are expressions into an expression of Map. */
def ofMapKeys[K: Type, V: Type: Liftable](m: Map[Expr[K], V])(
using QuoteContext): Expr[Map[K, V]] =
ofMapKeyValues(m.map { case (k, v) => (k, Expr(v)) })

/** Lifts the Map the keys and values of which are expressions into an expression of Map. */
def ofMapKeyValues[K: Type, V: Type](m: Map[Expr[K], Expr[V]])(
using QuoteContext): Expr[Map[K, V]] =
val listOfExprs: List[Expr[(K, V)]] =
m.iterator.map((k, v) => '{ ($k, $v) }).toList
val exprOfList: Expr[List[(K, V)]] = Expr.ofList(listOfExprs)
'{ Map.from($exprOfList) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use Map.apply and pass the varargs directly as done in ofList.

end ofMapKeyValues

/** Lifts this sequence of expressions into an expression of a tuple
*
* Transforms a sequence of expression
Expand Down
3 changes: 3 additions & 0 deletions tests/run-macros/expr-ofMap.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
List((a,b), (foo,bar))
List((a,b), (foo,bar))
List((a,b), (foo,bar))
22 changes: 22 additions & 0 deletions tests/run-macros/expr-ofMap/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import scala.quoted._

inline def ofMapKeyValues: Map[String, String] = ${ ofMapKeyValuesImpl }
private def ofMapKeyValuesImpl(using QuoteContext): Expr[Map[String, String]] =
Expr.ofMapKeyValues(Map(
'{ "foo" } -> '{ "bar" },
'{ "a" } -> '{ "b" }
))

inline def ofMapKeys: Map[String, String] = ${ ofMapKeysImpl }
private def ofMapKeysImpl(using QuoteContext): Expr[Map[String, String]] =
Expr.ofMapKeys(Map(
'{ "foo" } -> "bar",
'{ "a" } -> "b"
))

inline def ofMapValues: Map[String, String] = ${ ofMapValuesImpl }
private def ofMapValuesImpl(using QuoteContext): Expr[Map[String, String]] =
Expr.ofMapValues(Map(
"foo" -> '{ "bar" },
"a" -> '{ "b" }
))
4 changes: 4 additions & 0 deletions tests/run-macros/expr-ofMap/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@main def Test =
println(ofMapKeyValues.toList.sortBy(_._1))
println(ofMapKeys.toList.sortBy(_._1))
println(ofMapValues.toList.sortBy(_._1))