Search code examples
scalamacrosscala-macrosscala-3

Get information of type in Scala3 macro


I'm struggling to get information of type in Scala3 macro implementation. I'll explain problem through code.

Here is application logic:

object BlockServiceImpl extends BlockService:
  def authenticateUser0() = new ServiceCall[AuthUser,AuthUserResponse]:
     def invoke(request: AuthUser): Future[AuthUserResponse] = 
       println("BlockServiceImpl authenticateUser0 called")
       Future.successful(AuthUserResponse("test"))   

Now, for the logic I want to make endpoints with help of macro.

defineRoute("POST","/v1/block",BlockServiceImpl.authenticateUser0)

This is inline method:

inline def defineRoute[Q: RootJsonFormat ,R: RootJsonFormat](method: String, uri: String,inline call: () =>  ServiceCall[Q,R]): AkkaHttpCall = ${ methodImpl[Q,R]('uri, 'call)}

And this is implementation of macro:

def methodImpl[Q: Type,R: Type](uri: Expr[String],expr: Expr[Function0[ServiceCall[Q,R]]])(using ctx: Quotes): Expr[AkkaHttpCall] = ...

How can I get information that Q is AuthUser type during macro expansion in a compile time?


Solution

  • I've solved it by putting information if Q and R were special cases (NotUsed, Done,..) in serializers for Q and R. The idea is took from the Lagom framework.