1

Can I use dottymacro staging to parse a string, generate code and run it at runtime?

I was thinking about build a schema and web query language that can be used in different ways: passed via REST, or statically constructed and verified at compile-time. This will require both modes of operation (runtime/compile-time) and being able to reuse parts of the macro code in both modes.

From my first analysis of dotty lang, it looks feasible; however, I haven't seen any way of parsing a string using a function signature like String => Expr[?]. Is this possible?

Also, does these macros have the power to change the scala code semantics? For instance, I want to do something like this, directly in scala code:

val preparedQuery = db.query User | name == "Alex" | page 1 limit 10 {
  (asc 1) name,
  address { country, city }
  roles { * }
}

I should also be able to perform the same query using a string:

val preparedQuery = db.parseQuery(" <the same query> ")
Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
shumy
  • 343
  • 1
  • 2
  • 10
  • Macros can't be used to change the grammar of Scala itself, but you can still make your own DSL and write stuff very similar to those queries you want – user Jun 15 '20 at 18:11
  • As for whether or not you can use the same code to generate code at compile time or parse stuff at runtime, I don't think you can. See the docs for [Multi-Stage Programming](https://dotty.epfl.ch/docs/reference/metaprogramming/staging.html) – user Jun 15 '20 at 18:23

0 Answers0