essential-macros icon indicating copy to clipboard operation
essential-macros copied to clipboard

A sample that uses method parameters?

Open kostaskougios opened this issue 10 years ago • 3 comments

Hi, it would be nice to have a sample macro that uses method arguments,i.e. A macro that proxies all methods of a trait and calls a function passing a list of all params.

Btw nice examples and really helpful.

kostaskougios avatar Apr 21 '15 21:04 kostaskougios

Hi Kostas,

Sounds like a great idea! I'll put it on the TODO list but I'm not sure when I'll have time... PRs accepted if you're willing :)

Best regards,

Dave

On Tue, Apr 21, 2015 at 10:07 PM Kostas Kougios [email protected] wrote:

Hi, it would be nice to have a sample macro that uses method arguments,i.e. A macro that proxies all methods of a trait and calls a function passing a list of all params.

Btw nice examples and really helpful.

Reply to this email directly or view it on GitHub https://github.com/underscoreio/essential-macros/issues/2.

davegurnell avatar Apr 22 '15 10:04 davegurnell

I am about to do that but got stuck in an issue. I can't seem to get each method parameters to a list so that I can pass them to the proxy. This is the last step for the proxy macro to work but didn't manage to impl it yet. My code so far is

packagemacrotests

importscala.language.experimental.macros importscala.reflect.macros.whitebox.Context

/**

  • A demo macro that proxies traits and calls a function for each method of a trait * *@authorkostas.kougios */ objectProxyMacro { typeImplementor= (String,Any) => Any

    defproxyT:T=macroimpl[T]

    defimpl[T: c.WeakTypeTag](c: Context)(implementor: c.Expr[Implementor]): c.Expr[T] = { importc.universe._

    valtpe = weakTypeOf[T]

    valdecls = tpe.decls.map { decl => valtermName = decl.name.toTermName valmethod = decl.asMethod valparams = method.paramLists.map(.map(s => internal.valDef(s))) valparamVars = method.paramLists.map(.map { s => internal.captureVariable(s) internal.referenceCapturedVariable(s) }).flatten

      q""" def$termName  (...$params) = {
         $implementor  (${termName.toString}, ...${method.paramLists}  ).asInstanceOf[${method.returnType}]
         }"""
    

    }

    c.Expr[T] { q""" new$tpe { ..$decls } """ } } }

but gives a compilation error:

Error:(32, 53) Can't unquote List[List[c.universe.Symbol]] with ..., consider omitting the dots or providing an implicit instance of Liftable[c.universe.Symbol] $implementor (${termName.toString}, ...${method.paramLists} ).asInstanceOf[${method.returnType}] ^

Cheers On 22/04/15 11:07, Dave Gurnell wrote:

Hi Kostas,

Sounds like a great idea! I'll put it on the TODO list but I'm not sure when I'll have time... PRs accepted if you're willing :)

Best regards,

Dave

On Tue, Apr 21, 2015 at 10:07 PM Kostas Kougios [email protected] wrote:

Hi, it would be nice to have a sample macro that uses method arguments,i.e. A macro that proxies all methods of a trait and calls a function passing a list of all params.

Btw nice examples and really helpful.

Reply to this email directly or view it on GitHub https://github.com/underscoreio/essential-macros/issues/2.

— Reply to this email directly or view it on GitHub https://github.com/underscoreio/essential-macros/issues/2#issuecomment-95115643.

kostaskougios avatar Apr 25 '15 18:04 kostaskougios

Hi Kostas,

I'm not sure but you may need a List[List[ValDef]]. Here's a Stack Overflow thread that seems to be doing something similar:

http://stackoverflow.com/questions/18559559/quasiquotes-for-multiple-parameters-and-parameter-lists

I suggest asking questions like this on Stack Overflow or the scala-macros mailing list -- you're more likely to get a useful answer there. My knowledge of macros is rudimentary at best -- there are plenty of people out there who know loads more.

If you do ask elsewhere, please post a link to the question here in case others may find it useful.

Best regards,

Dave

On Sat, Apr 25, 2015 at 7:35 PM Kostas Kougios [email protected] wrote:

I am about to do that but got stuck in an issue. I can't seem to get each method parameters to a list so that I can pass them to the proxy. This is the last step for the proxy macro to work but didn't manage to impl it yet. My code so far is

packagemacrotests

importscala.language.experimental.macros importscala.reflect.macros.whitebox.Context

/**

  • A demo macro that proxies traits and calls a function for each method of a trait * *@authorkostas.kougios */ objectProxyMacro { typeImplementor= (String,Any) => Any

defproxyT:T=macroimpl[T]

defimpl[T: c.WeakTypeTag](c: Context)(implementor: c.Expr[Implementor]): c.Expr[T] = { importc.universe._

valtpe = weakTypeOf[T]

valdecls = tpe.decls.map { decl => valtermName = decl.name.toTermName valmethod = decl.asMethod valparams = method.paramLists.map(.map(s => internal.valDef(s))) valparamVars = method.paramLists.map(.map { s => internal.captureVariable(s) internal.referenceCapturedVariable(s) }).flatten

q""" def$termName (...$params) = { $implementor (${termName.toString}, ...${method.paramLists} ).asInstanceOf[${method.returnType}] }""" }

c.Expr[T] { q""" new$tpe { ..$decls } """ } } }

but gives a compilation error:

Error:(32, 53) Can't unquote List[List[c.universe.Symbol]] with ..., consider omitting the dots or providing an implicit instance of Liftable[c.universe.Symbol] $implementor (${termName.toString}, ...${method.paramLists} ).asInstanceOf[${method.returnType}] ^

Cheers On 22/04/15 11:07, Dave Gurnell wrote:

Hi Kostas,

Sounds like a great idea! I'll put it on the TODO list but I'm not sure when I'll have time... PRs accepted if you're willing :)

Best regards,

Dave

On Tue, Apr 21, 2015 at 10:07 PM Kostas Kougios < [email protected]> wrote:

Hi, it would be nice to have a sample macro that uses method arguments,i.e. A macro that proxies all methods of a trait and calls a function passing a list of all params.

Btw nice examples and really helpful.

Reply to this email directly or view it on GitHub https://github.com/underscoreio/essential-macros/issues/2.

Reply to this email directly or view it on GitHub < https://github.com/underscoreio/essential-macros/issues/2#issuecomment-95115643 .

Reply to this email directly or view it on GitHub https://github.com/underscoreio/essential-macros/issues/2#issuecomment-96258353 .

davegurnell avatar Apr 27 '15 07:04 davegurnell