0

I'll get straight to the business.

Let's say that I have the following trait definition:

trait Routable{
    def routing(): String
}

And I'm defining the following class:

case class MyEvent(name: String, age: Int) extends Routable{
    override def routing(): String = "this is my routing key"
}

I'm trying to make a macro called routeOf[MyEvent] to return the routing key of the defined class.

I tried so many things for the last 3 days and I'm starting to wonder if it is possible at all...

My macro definition is:

def routeOf[T]: Any = macro RouteOfMacro.impl[T] def impl[T: c.WeakTypeTag](c: whitebox.Context): c.Tree

But I can't find how to extract the method from the WeakTypeTag (and the internet is not full with examples).

So can it be done?

user1396033
  • 215
  • 3
  • 11

1 Answers1

0

I'm trying to make a macro called routeOf[MyEvent] to return the routing key of the defined class.

The way your code is, it's not the routing key of the defined class, but of the instance.

If you create multiple MyEvent, there is no guarantee their routing keys would be alike.

What you can do is create a companion object MyEvent that derives from Routable. I feel classTag or typeTag may be enough for this case - do you know those? No macros should be needed. But until I know the larger picture, hard to say how I'd approach it. Can you reveal more? :)

akauppi
  • 17,018
  • 15
  • 95
  • 120