Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8966229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:58:54+00:00 2026-06-15T16:58:54+00:00

Is there a convenient way to turn a MethodSymbol into the left-hand side of

  • 0

Is there a convenient way to turn a MethodSymbol into the left-hand side of a method definition tree (i.e., a DefDef) in Scala 2.10?

For example, suppose I want to create a macro that will take an instance of a trait and wrap all of that trait’s methods with some debugging functionality. I can write the following:

import scala.language.experimental.macros
import scala.reflect.macros.Context

object WrapperExample {
  def wrap[A](a: A): A = macro wrap_impl[A]

  def wrap_impl[A: c.WeakTypeTag](c: Context)(a: c.Expr[A]) = {
    import c.universe._

    val wrapped = weakTypeOf[A]
    val f = Select(reify(Predef).tree, "println")

    val methods = wrapped.declarations.collect {
      case m: MethodSymbol if !m.isConstructor => DefDef(
        Modifiers(Flag.OVERRIDE),
        m.name,
        Nil, Nil,
        TypeTree(),
        Block(
          Apply(f, c.literal("Calling: " + m.name.decoded).tree :: Nil),
          Select(a.tree, m.name)
        )
      )
    }.toList

  //...
}

I’ve elided the boring business of sticking these methods in a new anonymous class that implements the trait and then instantiating that class—you can find a complete working example here if you’re interested.

Now I can write this, for example:

scala> trait X { def foo = 1; def bar = 'a }
defined trait X

scala> val x = new X {}
x: X = $anon$1@15dd533

scala> val w: X = WrapperExample.wrap[X](x)
w: X = $1$$1@27c3a4a3

scala> w.foo
Calling: foo
res0: Int = 1

scala> w.bar
Calling: bar
res1: Symbol = 'a

So it works, but only in very simple cases—it won’t if the trait has methods with parameter lists, with access modifiers, annotations, etc.

What I really want is a function that will take a method symbol and a tree for the new body and return a DefDef. I’ve started writing one by hand, but it involves a lot of fiddly stuff like this:

List(if (method.isImplicit) Some(Flag.IMPLICIT) else None, ...)

Which is annoying, verbose, and error-prone. Am I missing some nicer way to do this in the new Reflection API?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:58:55+00:00Added an answer on June 15, 2026 at 4:58 pm

    To the best of my knowledge, there’s no standard way to go from a symbol to a defining tree.

    Your best bet would probably be to iterate through c.enclosingRun.units, recursing into each of the unit.body trees as you go. If you see a DefDef, which has a symbol equal to your symbol, then you’ve reached your destination. upd. Don’t forget to duplicate the defining tree before reusing it!

    This technique is far from being the most convenient thing in the world, but it should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there some convenient way to convert array [a,b,c] to hash-tree structure but only
Is there a convenient way to calculate percentiles for a sequence or single-dimensional numpy
Is there a convenient way to do this for my extension? I know I
Is there any convenient way to type in braces in Math in Latex? Especially
Is there any convenient way to take an array/set of objects and create a
Does anyone know if there's a short-hand or convient way of disabling all of
in languages like PHP or Python there are convenient functions to turn an input
For a machine with multiple NIC cards, is there a convenient method in Java
Is there any convenient way to read and parse data from incoming request. E.g
Is there a convenient way to get mouse deltas (e.g. mickeys) under X/linux? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.