JuliaIO / GZip.jl

A Julia interface for gzip functions in zlib
https://juliaio.github.io/GZip.jl/dev
MIT License
39 stars 30 forks source link

support for `do` blocks in GZip `open` functions? #88

Closed RTbecard closed 9 months ago

RTbecard commented 3 years ago

For Julia's Base.open, you can call it from a do block which automatically closes the file at the end and makes your code a little neater.

open("outfile", "w") do io
    write(io, data)
end

This behaves the same as python's recommended way of reading files.

with open('file', 'w') as f:
    f.write("content")

The source code for this is super simple.

function open(f::Function, args...)
    io = open(args...)
    try
        f(io)
    finally
        close(io)
    end
end

Is there any reason this functionality coulden't be implemented here? Or perhaps theres design reasons against it? If not, I'd be interested in making a pull request (so long as this is as easy as I think it looks).

m-wells commented 3 years ago

Doesn't this already work?

RTbecard commented 3 years ago

oh, yup, yes it is. My bad :/

I never tried it, as I didn't see anything in the docs referring to it or descriptions of the open methods which accepted a function as a first argument.