Referencing local variable before assignment results in unexpected behavior

This is interesting, because just recently I’ve used this feature and I was so thankful, that it exist.

The task was to parse contents of some zip file, which on its own was a mess of nested subdirectories and some of those had files which I needed. So I did something like this

function parsezip(f, zipfile)
# some logic which opens file, go through the nested file tree 
# and finally catch necessary file
...
  x = unpack_contents(file)
  f(x)
# some other logic which process errors if they happened and finally gracefully close the file
end

Now, in my actual processing part I did something like this

function process(zipfile)
  v = [] # surely it wasn't `Any[]`, but it's not important here
  parsezip(zipfile) do x
    # some logic to convert contents to DataFrame and postprocessing
    ....
    data = .... # finally I get my data
    push!(v, data)
  end
  return v
end

And that’s it! Logic of traversing zipfile and data content parsing was separated, I get what I need and it was totally aligned with my mental model of what is happening. Probably one can do it better (write iterators or forcing to pass parameters or something else), but the fact that it worked as it is was really great.

4 Likes