Can this be done with Scala macros?

Eran Medan

I wonder if this can be done with scala macros

Consider this method:

def doSomething(filter: Item => Boolean) = ...

class Item(properties: Map[String, String]) extends Dynamic {
  def selectDynamic(name:String): String = {
    properties.getOrElse(name, "")
  }
  def updateDynamic(name: String)(value: String): Unit = {
    addProperty(name, value)
  }
}

And this usage

doSomething(x=> x.foo == "X" && x.bar  == "Y" || x.baz.nonEmpty)

What I'd like to do is to simplify it (I'm writing a DSL for people who don't really use scala much) to this:

doSomething(foo == "X" && bar  == "Y" || baz.nonEmpty)

My assumption is that even with Scala macros this might be impossible, or is it?

If so, where do I start? I assume this is not a simplistic macro...

som-snytt

You're right that the identifiers must be introduced.

In his ugliest code and worst pun on Metaplasm, Travis Brown called it "Potemkin val-age". A macro introduces the syntax machinery, which is imported before use (or abuse).

Potemkin definitions may not suit your use case if your property names are just arbitrary data.

Maybe something like this to collect the syntax, then a macro could operate on it (as opposed to the quickie conversion here).

case object is

object when extends Dynamic {
  def applyDynamic(f: String)(op: Any) = new P(f, op)
}

case class P(f: String, op: Any) extends Dynamic {
  def applyDynamic(op: String)(value: String) = Q(this, op, value)
}
case class Q(p: P, op: String, value: String)

object Test extends App {
  implicit def `Q to filter`(q: Q): Item => Boolean = (i: Item) => (q.p.op, q.op) match {
    case (is, "equal") => i.properties.get(q.p.f) map (_ == q.value) getOrElse false
    case _  => false
  }
  val items = List (
    Item(Map("foo" -> "X", "bar" -> "Y")),
    Item(Map("this" -> "that")),
    Item(Map("baz" -> "more"))
  )
  def doSomething(filter: Item => Boolean) = Console println (items filter filter)

  doSomething(when foo is equal "X")
}

I didn't give Dynamic a thought until I saw this earlier today: https://stackoverflow.com/a/16256935/1296806

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create variables with macros in Scala

From Dev

Splicing together symbols with Scala macros

From Dev

Scala Macros: Accessing members with quasiquotes

From Dev

Scala macros and separate compilation units

From Dev

Non-scala source positions with scala macros

From Dev

How to generate a "qualified Select" using Scala macros?

From Dev

(In Scala,) Is there anything that can be done with generic type parameters of classes but not with abstract type members?

From Dev

Can't expand macros compiled by previous versions of Scala (scala 2.11.4, sbt 0.13.7, JDK 8)

From Dev

Scala code generation with annotations + macros or external script?

From Dev

scala macros generating implicits

From Dev

Can be done more pythonic?

From Dev

Scala macros referring to a member type

From Dev

SBT 0.13.0 - can't expand macros compiled by previous versions of Scala

From Dev

How can I reuse code between Javascript macros and minimize work done within the macros?

From Dev

"debugging" scala annotation macros

From Dev

Can this be done with regex?

From Dev

What can you do with macros that can't be done with procedures?

From Dev

Can this be done with SQLAhclemy / Python?

From Dev

Can this be done faster with numpy?

From Dev

Can this be done with STM?

From Dev

Debug Scala Macros with Intellij

From Dev

Non-scala source positions with scala macros

From Dev

Matching generic types in Scala macros

From Dev

Can't expand macros compiled by previous versions of Scala (scala 2.11.4, sbt 0.13.7, JDK 8)

From Dev

Scala macros referring to a member type

From Dev

How can I get Scala Named and Default arguments to work with macros

From Dev

Can this be done with regex?

From Dev

Using Scala macros, can i make implementer of method call super?

From Dev

What can you do with macros that can't be done with procedures?

Related Related

  1. 1

    How to create variables with macros in Scala

  2. 2

    Splicing together symbols with Scala macros

  3. 3

    Scala Macros: Accessing members with quasiquotes

  4. 4

    Scala macros and separate compilation units

  5. 5

    Non-scala source positions with scala macros

  6. 6

    How to generate a "qualified Select" using Scala macros?

  7. 7

    (In Scala,) Is there anything that can be done with generic type parameters of classes but not with abstract type members?

  8. 8

    Can't expand macros compiled by previous versions of Scala (scala 2.11.4, sbt 0.13.7, JDK 8)

  9. 9

    Scala code generation with annotations + macros or external script?

  10. 10

    scala macros generating implicits

  11. 11

    Can be done more pythonic?

  12. 12

    Scala macros referring to a member type

  13. 13

    SBT 0.13.0 - can't expand macros compiled by previous versions of Scala

  14. 14

    How can I reuse code between Javascript macros and minimize work done within the macros?

  15. 15

    "debugging" scala annotation macros

  16. 16

    Can this be done with regex?

  17. 17

    What can you do with macros that can't be done with procedures?

  18. 18

    Can this be done with SQLAhclemy / Python?

  19. 19

    Can this be done faster with numpy?

  20. 20

    Can this be done with STM?

  21. 21

    Debug Scala Macros with Intellij

  22. 22

    Non-scala source positions with scala macros

  23. 23

    Matching generic types in Scala macros

  24. 24

    Can't expand macros compiled by previous versions of Scala (scala 2.11.4, sbt 0.13.7, JDK 8)

  25. 25

    Scala macros referring to a member type

  26. 26

    How can I get Scala Named and Default arguments to work with macros

  27. 27

    Can this be done with regex?

  28. 28

    Using Scala macros, can i make implementer of method call super?

  29. 29

    What can you do with macros that can't be done with procedures?

HotTag

Archive