Generating multithreaded functions?

Is it possible to somehow generate functions with a multithreaded for-loop with @threads macro (or any other construct with the same fuctionality)?

The straightforward way fails

julia> @generated function f(x::Integer)
       quote
           @threads for i = 1:x
               println(i)
           end
       end
   end

julia> f(5)
ERROR: generated function body is not pure. this likely means it contains a closure or comprehension.

Would it be possible to make the inner function generated?
E.g.

function f(x::Integer)
 @threads for i = 1:x
       f_generated_inner(x, i)
  end
end
2 Likes

That’s a brilliant idea, however there is also an “initialization” part and “return” part of the function, which also depend on input types. I omitted these in the MWE above.