You can create a custom type to store the old solution
# Likelihood function
mutable struct LogLik
W
end
function (l::LogLik)(param)
W_new = VFI(param, l.W)
l.W = W_new
return -log(F(W_new,param)) # F is some function
end
# Optimizer
loglik = LogLik(nothing)
outmax = optimize(loglik(x), x0) ;