3 ms·
No, it's really just an iterator in Julia 0.7 or 1.0. At the same time it's just a function call. for x in itr println(x) end Is exactly equival
by stabbles 8y ago
No, it's really just an iterator in Julia 0.7 or 1.0. At the same time it's just a function call.
for x in itr
println(x)
end
Is exactly equivalent to and syntactic sugar for
y = iterate(itr)
while y !== nothing
x, state = y
println(x)
y = iterate(itr, state)
end
What makes this interesting is two things:
1. It uses Julia's multiple dispatch: every iterator has to implement or extend the function `iterate(itr::MyIteratorType, state...) = ...`
2. Julia can handle small union types efficiently. In the example above the inferred type of `y` will be `Union{Nothing,Tuple{...,...}}`. The compiler will generate efficient code -- in previous versions of Julia any `Union` type would result in a heap allocation and `y` would internally be a pointer. In Julia 0.7 there's clever code generation and no allocations.