question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Quotes: generate arbitrary trait or class implementations

See original GitHub issue

The problem

Using Scala 2 macros, we have built an RPC framework that can automatically generate implementations of arbitrary traits. These implementations are effectively network proxies that translate every method call into a network request. When the request is received by the RPC server, a reverse translation is performed and a real implementation of this trait is invoked.

A very simplified example of this mechanism:

trait RawRpc {
  def invoke(methodName: String, parameters: Map[String, Json]): Future[Json]
}
object RawRpc {
  def asReal[RealRpc](rawRpc: RawRpc): RealRpc = macro ...
  def asRaw[RealRpc](realRpc: RealRpc): RawRpc = macro ...
}

The asReal macro needs to do the following:

  • enumerate all (abstract) methods of RealRpc
  • for every parameter of every method, find a typeclass instance that would allow it to serialize it into Json
  • for every method’s result type, find a typeclass instance that would allow it to deserialize it from Json (assuming that the result type is a Future)
  • generate an implementation of every method that forwards it to rawRpc
  • inspect annotations on methods and parameters in order to treat them in some special way

The asRaw macro needs similar capabilities in order to perform a reverse translation.

Situation in Scala 3

It’s unclear to me whether something like this is possible with Scala 3. Looking at the Quotes API, it should in principle be possible if I was able to generate arbitrary trees, starting with ClassDef. However, it seems that currently ClassDef.apply is commented out and marked as TODO.

So my questions are:

  • First of all, is this a valid use case for Scala 3 metaprogramming?
  • If yes, can this be done with Quotes API?
  • Are there any serious obstacles preventing methods like ClassDef.apply from being exposed?

(Note: I asked this question briefly on Gitter and it was recommended to me to create an issue.)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:21
  • Comments:30 (16 by maintainers)

github_iconTop GitHub Comments

9reactions
jdegoescommented, Jan 28, 2022

My company is interested in sponsoring a production-worthy and well-thought out implementation of this feature.

2reactions
smartercommented, Apr 20, 2022

when do you expect this will be released?

We try to not rush stabilizing APIs since we’re bound by binary compatibility constraints afterwards, but one thing that would help would be to get feedback from users on whether the APIs implemented in https://github.com/lampepfl/dotty/pull/14124 actually solve their problems (see the PR changes for documentation and test cases).

To try it out, just set your scalaVersion to a recent nightly to have access to experimental APIs (scroll to the bottom of https://repo1.maven.org/maven2/org/scala-lang/scala3-compiler_3/ to find the version number), e.g.

scalaVersion := "3.2.0-RC1-bin-20220419-ef16034-NIGHTLY"
Read more comments on GitHub >

github_iconTop Results From Across the Web

quote - Rust - Docs.rs
Procedural macros in Rust receive a stream of tokens as input, execute arbitrary Rust code to determine how to manipulate those tokens, and...
Read more >
Quoted Code | Macros in Scala 3
So far, we assumed that the types within quote patterns would be statically known. Quote patterns also allow for generic types and existential...
Read more >
Macros - Scala 3
Macros: Quotes and Splices. Macros are built on two well-known fundamental operations: quotation and splicing. Quotation is expressed as '{.
Read more >
c++ - Implement a type trait for a class template that is true for ...
4 Answers 4 ; 4 · C++20 concepts make things much easier: template ; 1 · You can do it this way: #include...
Read more >
Scala 3 macros tips & tricks - SoftwareMill
Some of the things we've learned when implementing a number of macros using Scala 3. A handy guide to start metaprogramming with Scala....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found