# Concise multiple do-block

**URL:** <https://discourse.julialang.org/t/concise-multiple-do-block/7736>\
**Category:** General Usage\
**Created:** [December 13, 2017, 12:21pm UTC](https://discourse.julialang.org/t/concise-multiple-do-block/7736 "2017-12-13T12:21:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidavdav](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidavdav/32/1065_2.png) [@davidavdav](https://discourse.julialang.org/u/davidavdav)\
**Post date:** [December 13, 2017, 12:21pm UTC](https://discourse.julialang.org/t/concise-multiple-do-block/7736/1 "2017-12-13T12:21:14Z")

</div>

Hello,

Is there a concise way of opening multiple files simultaneously in a do-block construct? This only to save indents, really.

```julia
open("one") do fd1
   open("two") do fd2
       open("out", "w") do out
           ## lots of indent for using fd1, fd, out
       end
   end
end

```

Thanks,  
—david

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [December 13, 2017, 2:33pm UTC](https://discourse.julialang.org/t/concise-multiple-do-block/7736/2 "2017-12-13T14:33:47Z")

</div>

I’ve recently been running into the same thing. Probably not the best, but the solution I’ve been using is:

```julia
open("one") do fd1; open("two") do fd2; open("out", "w") do out
     # do stuff with one indent because whitespace doesn't actually matter!
end; end; end

```

Not the prettiest solution, but it works! (note: you don’t actually even need the semicolons, but it makes things a bit clearer to me)

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [December 13, 2017, 5:29pm UTC](https://discourse.julialang.org/t/concise-multiple-do-block/7736/3 "2017-12-13T17:29:50Z")

</div>

Lisp-y recursion comes in pretty handy in this case:

```julia

Main> openall(f::Function, file::AbstractString, other_files...) =
           openall(f, (file, "r"), other_files...)
openall (generic function with 1 method)

Main> function openall(f::Function, file::Tuple, other_files...)
           open(file...) do handle
               openall(other_files...) do handles...
                   f(handle, handles...)
               end
           end
       end
openall (generic function with 2 methods)

Main> openall(f::Function) = f()
openall (generic function with 3 methods)

Main> openall("f1") do f1
           @show f1
       end
f1 = IOStream(<file f1>)
IOStream(<file f1>)

Main> openall("f1", ("f2", "w")) do f1, f2
           @show f1 f2
       end
f1 = IOStream(<file f1>)
f2 = IOStream(<file f2>)
IOStream(<file f2>)

Main> openall("f1", ("f2", "a"), "f3", ("f4", "w")) do f1, f2, f3, f4
           @show f1 f2 f3 f4
       end
f1 = IOStream(<file f1>)
f2 = IOStream(<file f2>)
f3 = IOStream(<file f3>)
f4 = IOStream(<file f4>)
IOStream(<file f4>)

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 14, 2017, 12:40am UTC](https://discourse.julialang.org/t/concise-multiple-do-block/7736/4 "2017-12-14T00:40:39Z")

</div>

This proposed feature would help quite a bit:

[https://github.com/JuliaLang/julia/issues/7721#issuecomment-171345256](https://github.com/JuliaLang/julia/issues/7721#issuecomment-171345256)

You could write this particular example as:

```julia
fd1 = open("one")!
fd2 = open("two")!
out = open("out", "w")!
## no indent for using fd1, fd, out
# all handles closed when scope ends

```

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [December 14, 2017, 11:00am UTC](https://discourse.julialang.org/t/concise-multiple-do-block/7736/5 "2017-12-14T11:00:52Z")

</div>

Nice. What I like about the `f(x)!` syntax is that code that works with the exclamation mark at the end of the function will (most of the time?) work without it as well, making this syntax flexible and forgiving and therefore easier to adopt.

---

<div class="post-metadata">

**Author:** ![davidavdav](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidavdav/32/1065_2.png) [@davidavdav](https://discourse.julialang.org/u/davidavdav)\
**Post date:** [December 14, 2017, 11:18am UTC](https://discourse.julialang.org/t/concise-multiple-do-block/7736/6 "2017-12-14T11:18:13Z")

</div>

Ah, yes, I remember this discussion from a while ago now. Has anyone picked up implementing the `()!` construct?

In [another thread](https://discourse.julialang.org/t/strange-interaction-between-channel-and-pipes/7623/8) I am busy with trying to figure out how to make a python-style generator that produces `IO` objects. The idea would be that the generator creates and opens the `IO`, and the caller/consumer uses the object until the next item. Then when the caller is done with the object, the GC should come in and trigger a `close()` on the `IO` object. (If this would be the mechanism, the GC should probably also be triggered by a low-on-filedescriptors event).
