If I understand correctly, the trick is to encode the dimension into the type of the arguments (using parametric types), then the @generated macro allows you to "write" a function just when the type of the arguments is known, so you can create the right numbers of loops on the fly.
http://julia.readthedocs.org/en/latest/manual/metaprogramming/#generated-functions Maybe this is a good example: https://github.com/JuliaLang/julia/blob/master/base/multidimensional.jl#L738
