Dagger.jl
Dagger.jl copied to clipboard
`iterate` for `Thunk`s
Currently if you try to unpack a result from delayed into several bindings on the lhs
julia> using Dagger
julia> swap(a, b) = b, a
swap (generic function with 1 method)
julia> x, y = delayed(swap)(1, 2)
MethodError: no method matching iterate(::Thunk)
Closest candidates are:
iterate(::Union{LinRange, StepRangeLen}) at range.jl:664
iterate(::Union{LinRange, StepRangeLen}, ::Int64) at range.jl:664
iterate(::T) where T<:Union{Base.KeySet{var"#s79", var"#s78"} where {var"#s79", var"#s78"<:Dict}, Base.ValueIterator{var"#s77"} where var"#s77"<:Dict} at dict.jl:693
...
you end up with a method error since delayed just returns a single Thunk.
Would it make sense to add an iterate method for Thunk such that you can decompose these calls correctly? From a brief test the following appears to work alright.
julia> Base.iterate(t::Thunk, index::Integer=1) = delayed(first ∘ iterate)(t, index), index + 1
julia> x, y = delayed(swap)(1, 2)
Thunk(swap, (1, 2))
julia> collect.((x, y))
(2, 1)
Yep, sounds good to me!