Is there a workaround for Scala error SI-7914 - returning an application method from a Scala macro?

In our library, we have a macro defined like this:

def extractNamed[A]: Any = macro NamedExtractSyntax.extractNamedImpl[A]

      

This macro uses its type signature to return a method apply

whose arguments match the method apply

of the case class on which it is typed.

The idea is that this allows the user of our library to use Scala named parameters, for example:

lazy val fruitExtractor = extractNamed[Fruit](
  name = FruitTable.name,
  juiciness = FruitTable.juiciness
)

      

However, this doesn't seem to work - it is returned error: Any does not take parameters

at compile time instead .

It looks like this is caused by SI-7914 as it works if the user explicitly invokes the request, that is:

lazy val fruitExtractor = extractNamed[Fruit].apply(
  name = FruitTable.name,
  juiciness = FruitTable.juiciness
)

      

We can get around this by simply renaming the returned method to something sensible, but is there any other way to get around this without having to explicitly call the method?

+3


source to share


1 answer


How about returning a subclass Dynamic

from a macro? Then you can define applyDynamic

and applyDynamicNamed

to do what you want. For added safety at compile time, these methods xxxDynamic

can also be macros. I just checked and it works exactly with the syntax you want it to be.



+2


source







All Articles