Cartesian macros on type fields in Julia
<p>The <code>Base.Cartesian</code> module offers the <code>@nref</code> macro:</p>

<pre><code>using Base.Cartesian
i_1, i_2 = 1, 1
A = rand(3,3)
@nref 2 A i
</code></pre>

<p>This returns <code>A[1,1]</code>. However, <code>@nref</code> does not work with fields of a (custom) type:</p>

<pre><code>type Foo
    bar::Matrix
end

foo = Foo( rand(3,3) )

i_1, i_2 = 1, 1
@nref 2 foo.bar i
</code></pre>

<p>This results in an error:</p>

<pre><code>ERROR: MethodError: `_nref` has no method matching _nref(::Int64, ::Expr, ::Symbol)
Closest candidates are:
  _nref(::Int64, ::Symbol, ::Any)
</code></pre>

<p>The error seems reasonable as <code>foo.bar</code> is really the expression <code>getfield(foo, bar)</code>.
Wrapping the <code>@nref</code> in a function and passing <code>foo.bar</code> works:</p>

<pre><code>function baz(A)
i_1, i_2 = 1, 1
@nref 2 A i
end

baz(foo.bar)
</code></pre>

<p>But is there a way to make <code>@nref 2 foo.bar i</code> work?</p>