Thanks - that makes sense, and answers the performance part of the question.
I’m still a bit confused how/why SomeVec(5) in this codeblock prints 40x “I’m running!” (i.e. something is still ran), but at least I know to not see these as values as equal to the ones in the regular loop/comprehension.
using Random
mutable struct A a::Float64 end
step!(x::A) = x.a += rand()
function run()
println("I'm running!")
t = A(1.0)
while t.a < 2.0 step!(t) end
t.a
end
Random.seed!(1)
@time res = [run() for i in 1:5]
struct SomeVec <: AbstractVector{Float64} len::Int end
Base.size(s::SomeVec) = (s.len,)
Base.getindex(::SomeVec, i::Int) = run()
init = SomeVec(5) # run() prints stuff 40x
Random.seed!(1)
@time res2 = [init[i] for i in eachindex(init)]
res == res2 # true