title: Macro access to members of object where macro is defined
body: <p>Say I have a trait <code>Foo</code> that I instantiate with an initial value <code>i</code></p>
  
  <pre><code>val foo = new Foo(6) // class Foo(i: Int)
  </code></pre>
  
  <p>and I later call a <code>secondMethod</code> that in turn calls <code>myMacro</code></p>
  
  <pre><code>foo.secondMethod(7) // def secondMethod(j: Int) = macro myMacro 
  </code></pre>
  
  <p>then, how can <code>myMacro</code> find the initial value of <code>i</code> (6)?</p>
  
  <p>I didn't succeed with normal compilation reflection using <code>c.prefix</code>, <code>c.eval(...)</code> etc but instead found a 2-project solution:</p>
  
  <p>Project B:</p>
  
  <pre><code>object CompilationB {
      def resultB(x: Int, y: Int) = macro resultB_impl
      def resultB_impl(c: Context)(x: c.Expr[Int], y: c.Expr[Int]) =
        c.universe.reify(x.splice * y.splice)
  }
  </code></pre>
  
  <p>Project A (depends on project B):</p>
  
  <pre><code>trait Foo {
    val i: Int
  
    // Pass through `i` to compilation B:
    def apply(y: Int) = CompilationB.resultB(i, y)
  }
  
  object CompilationA {
    def makeFoo(x: Int): Foo = macro makeFoo_impl
    def makeFoo_impl(c: Context)(x: c.Expr[Int]): c.Expr[Foo] =
      c.universe.reify(new Foo {val i = x.splice})
  }
  </code></pre>
  
  <p>We can create a <code>Foo</code> and set the <code>i</code> value either with normal instantiation or with a macro like <code>makeFoo</code>. The second approach allows us to customize a <code>Foo</code> at compile time in the first compilation and then in the second compilation further customize its response to input (<code>i</code> in this case)! In some way we get "meta-meta" capabilities (or "pataphysic"-capabilities ;-)</p>
  
  <p>Normally we would need to have foo in scope to introspect <code>i</code> (with for instance c.eval(...)). But by saving the <code>i</code> value inside the <code>Foo</code> object we can access it anytime and we could instantiate <code>Foo</code> anywhere:</p>
  
  <pre><code>object Test extends App {
    import CompilationA._
  
    // Normal instantiation
    val foo1 = new Foo {val i = 7}
    val r1   = foo1(6)
  
    // Macro instantiation
    val foo2 = makeFoo(7)
    val r2   = foo2(6)
  
    // "Curried" invocation
    val r3 = makeFoo(6)(7)
  
    println(s"Result 1 2 3: $r1 $r2 $r3")
    assert((r1, r2, r3) ==(42, 42, 42))
  }
  </code></pre>
  
  <p><strong>My question</strong></p>
  
  <p>Can I find <code>i</code> inside my example macros without this double compilation hackery?</p>