While the Buffer “solution” does work, it complicates things and doesn’t make for very clean code, so I’d still like comprehension in general to work - but in most cases I still can’t make it.
Here’s an example where I can’t make it work, although it also uses a dictionary which might add another issue to the mix:
using Zygote
w = randn(5)
grads = gradient(Params(w)) do
dict = Dict{Int, Float64}(i => v for (i,v) in enumerate(w))
m = maximum(i*v for (i,v) in dict)
k = randn(5)
sum(k .- m)
end
results in MethodError: no method matching getindex(::Dict{Any,Any}). Besides, you only get this far with the generator syntax since the array comprehension syntax allocates an array and copies data into it, which is unsupported mutation. I’ve tried a bunch of variations on the maximum()
line to no avail.