jilen
jilen

Reputation: 5763

wrap a macro expansion method

I am trying to wrap the play json writes macro expansion

def encoder[T] = Json.writes[T]

But the T is unknown , so that won't compile, it complains

no unapply method found

I know I can replace Json.writes[T] with JsMacroImpl.reads expansion. Is there a better way doing this ?

Upvotes: 1

Views: 146

Answers (1)

tiran
tiran

Reputation: 2431

I faced a similar problem some time back. you can do something like this.

create an object and inside write encoder method as a macro method.

object SomeObj {
  def encoder[T] = macro encoderRedirect_impl[T]

  def encoderRedirect_impl[T : c.WeekTypeTag](c:Context) = {
    q"Json.writes[${c.weakTypeOf[T]}]"
  }
}

This is a macro redirect for my understanding. but you have to write this code in seperate project and use it as a dependency to your project.

Upvotes: 2

Related Questions