Slightly modifying an example from the docs: julia> function mysub2ind_gen(N) ex = :(I[$N] - 1) for i = N-1:-1:1 ex = :(I[$i] - 1 + dims[$i]*$ex) end return :($ex + 1) end mysub2ind_gen (generic function with 1 method)
julia> @generated function mysub2ind{N}(dims::NTuple{N}, I::Integer...) length(I) == N || error("wrong number of indexes") mysub2ind_gen(N) end mysub2ind (generic function with 1 method) julia> mysub2ind_gen(3) :(((I[1] - 1) + dims[1] * ((I[2] - 1) + dims[2] * (I[3] - 1))) + 1) julia> mysub2ind((5,5,5), 1, 2, 3) 56 julia> sub2ind((5,5,5), 1, 2, 3) 56 On Wednesday, February 10, 2016 03:31:15 AM Jeffrey Sarnoff wrote: > Tim -- would you repeat that with some simple content illustrative of a > useful use for generation --- thx > > On Wednesday, February 10, 2016 at 5:27:12 AM UTC-5, Tim Holy wrote: > > On Tuesday, February 09, 2016 08:52:22 PM Andy Ferris wrote: > > > What's the best way to find the code generated by a @generated function? > > > > This isn't easy unless you (or the author) provides a function to do so: > > @generated function foo(x, y) > > > > foo_generator(x, y) > > > > end > > > > function foo_generator{Tx,Ty}(::Type{Tx}, ::Type{Ty}) > > > > # generate and return the expression for the function body > > > > end > > > > Then you can call `foo_generator(typeof(x), typeof(y))` to see the > > returned > > code. > > > > Best, > > --Tim