1

I'll use the shapeless library as an example:

import shapeless.test.illTyped

assuming that I want to wrap the illType function in another function, I've tried 2 different ways to do this:

(by value)

  def shouldTypeError(v: String) = illTyped(v, ".*")

[ERROR] ... : exception during macro expansion: 
scala.MatchError: v (of class scala.reflect.internal.Trees$Ident)
    at shapeless.test.IllTypedMacros.applyImpl(typechecking.scala:43)

(by name)

  def shouldTypeError(v: => String) = illTyped(v, ".*")

[ERROR ... ]: exception during macro expansion: 
scala.MatchError: v (of class scala.reflect.internal.Trees$Ident)
    at shapeless.test.IllTypedMacros.applyImpl(typechecking.scala:43)

So none of them work as intended. Is this possible in the latest scala or dotty?

tribbloid
  • 4,026
  • 14
  • 64
  • 103
  • 4
    In Scala 3, you can use `inline` defs (and possibly combine them with macros). See [the metaprogramming section](https://dotty.epfl.ch/docs/reference/metaprogramming/toc.html) for more details. – user May 13 '21 at 23:55
  • Thanks a lot, good to know it works for dotty macro, how about scala2? – tribbloid May 14 '21 at 00:17

1 Answers1

2

No. Outer function must a macro too

def shouldTypeError(v: String): Unit = macro shouldTypeErrorImpl

def shouldTypeErrorImpl(c: blackbox.Context)(v: c.Tree): c.Tree = {
  import c.universe._
  q"""_root_.shapeless.test.illTyped($v, ".*")"""
}
Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66