[macros] how does implicit WeakTypeTags work in list of selection?

Is it possible to generate a list value selections ?


These two lines :

case class FooBar(id: Long, foo: Int, bar: String)
 printCall1(FooBar(737, 111, "bobo"))

displays “t.id = 737”

but if I call
printCall2(FooBar(737, 111, "bobo"))
I get the following error “value id is not a member of FooBar”

I can see that in printCall1 the c.Expr( Select(t.tree, m1.name) ) in line 10 is implicitly provided a WeakTypeTag[T].
Even when I materialize one and pass it explicitly in printCall2 and printCallList it doesn’t work.

Any idea on how to fix it ?

( here’s the real scenario where I’m trying to make this work)

Placing a debug println in your macro can reveal a lot.
For instance println(c.weakTypeOf[T].decls.filter(_.isTerm)) will print

  val id: Long;
  private[this] val id: Long;
  val foo: Int;
  private[this] val foo: Int;
  val bar: String;
  private[this] val bar: String;
  def <init>(id: Long,foo: Int,bar: String): FooBar;
  def copy(id: Long,foo: Int,bar: String): FooBar;
  def copy$default$1: Long @scala.annotation.unchecked.uncheckedVariance;
  def copy$default$2: Int @scala.annotation.unchecked.uncheckedVariance;
  def copy$default$3: String @scala.annotation.unchecked.uncheckedVariance;
  override def productPrefix: String;
  def productArity: Int;
  def productElement(x$1: Int): Any;
  override def productIterator: Iterator[Any];
  def canEqual(x$1: Any): Boolean;
  override def hashCode(): Int;
  override def toString(): String;
  override def equals(x$1: Any): Boolean

Meaning that m2 will actually be private[this] val id: Long which is private. And there’s a lot of other stuff in there which you probably don’t want either.

You probably want to do something like this:

val accessors = c.weakTypeOf[T].decls.filter(d => d.isMethod && d.asMethod.isCaseAccessor)
1 Like

Yes ! Thank you. This is indeed what I wanted to do !