Maintaining iterator protocol for both 0.6 and 0.7

What’s the best way to make the code work under both 0.6 and 0.7 given the new iterator protocol? Should I just wrap the code around with something like if VERSION < "0.7"?

I used to have this:

Base.start(d::CIDict) = start(d.dct)
Base.next(d::CIDict, i::Int) = next(d.dct, i)
Base.done(d::CIDict, i::Int) = done(d.dct, i)

and the new protocol seems to require this:

Base.iterate(d::CIDict) = iterate(d.dct)
Base.iterate(d::CIDict, i) = iterate(d.dct, i)

Just use the old protocol for as long as you want 0.6 compatibility. It still works in 0.7, or so I thought?

1 Like

It didn’t work and that’s why I started looking into it… Maybe I’m missing something else?

Where it failed:

for (k,v) in d
    # whatever     
end

Stack trace:

case insensitive dict: Error During Test at /Users/tomkwong/.julia/dev/SASLib/test/runtests.jl:55
  Got exception MethodError(done, (CIDict(:abc => 88, :def => 77), Base.LegacyIterationCompat{Dict{Symbol,Int64},Pair{Symbol,Int64},Int64}(false, :abc => 88, 5)), 0x0000000000006b0d) outside of a @test
  MethodError: no method matching done(::SASLib.CIDict{Symbol,Int64}, ::Base.LegacyIterationCompat{Dict{Symbol,Int64},Pair{Symbol,Int64},Int64})
  Closest candidates are:
    done(::I, !Matched::Base.LegacyIterationCompat{I,T,S}) where {I, T, S} at essentials.jl:870
    done(!Matched::Missings.EachReplaceMissing, ::Any) at /Users/tomkwong/.julia/packages/Missings/Fu1R/src/Missings.jl:304
    done(!Matched::Missings.EachFailMissing, ::Any) at /Users/tomkwong/.julia/packages/Missings/Fu1R/src/Missings.jl:427
    ...
  Stacktrace:
   [1] iterate at ./essentials.jl:827 [inlined]
   [2] iterate at ./essentials.jl:831 [inlined]
   [3] (::getfield(Main, Symbol("#testdict#19")))(::Symbol, ::Symbol, ::Symbol) at /Users/tomkwong/.julia/dev/SASLib/test/runtests.jl:78
   [4] macro expansion at /Users/tomkwong/.julia/dev/SASLib/test/runtests.jl:56 [inlined]
   [5] macro expansion at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v0.7/Test/src/Test.jl:1080 [inlined]
   [6] macro expansion at /Users/tomkwong/.julia/dev/SASLib/test/runtests.jl:20 [inlined]
   [7] macro expansion at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v0.7/Test/src/Test.jl:1080 [inlined]
   [8] top-level scope at /Users/tomkwong/.julia/dev/SASLib/test/runtests.jl:19 [inlined]

Yes, I’d recommend this. You can use femtocleaner to remove the compatibility definitions when you’re ready to drop 0.6.

1 Like