struct Cogg
x::Function
end
fin(data::AbstractArray)=begin
df=data .* 2
@inline (i::Int)->begin
#assume this body of function can not be inlined by julia automatically
df[i]>0
end
end
(cogg::Cogg)(n::Int)=begin
x=rand(n)
func=cogg.x
for i=1:n
func(i)
end
end
cogg=Cogg(fin)
cogg(100)
In julia v1.8, you can call @inline at the callsite as well
help?> @inline
Give a hint to the compiler that calls within block are worth inlining.
# The compiler will try to inline `f`
@inline f(...)
# The compiler will try to inline `f`, `g` and `+`
@inline f(...) + g(...)
│ Julia 1.8
│
│ The callsite annotation requires at least Julia 1.8.