and I fiiiinnnally read the last bit of your post and saw that you want this to use with autodiff and so none of these solutions are likely to work :-/ Although I suppose there’s no harm in trying.
The other thing I’d suggest is to write a loop inside the closure that iterates over the terms and accumulates the results