Nested do blocks?

Dear developers,

While learning Julia, one of the constructions that I found particularly marvellous is the do block. As comprehensions and where expressions can be nested, wouldn’t it make sense also to allow it for the do blocks? One possible solution could be that, for example, the code

function oe(f, g)
    f("oo")
    g("ee")
end

oe() do e
    println("g", e)
end do o
    println("f", o)
end

would produce

foo
gee

Thus, as suggested above, the addition of the functions defined by the do blocks to the beginning of the argument list would work from the inside out; I find this solution natural, though the opposite direction producing

goo
fee

might be more programmer-friendly.

I am aware that such an idea might already have appeared, possibly several times. :slightly_smiling_face: I should also be grateful for an explanation why nested do blocks are not a good idea.

Thank you very much for your time!

I’m not a developer, but this isn’t nesting, it’s a sequence of do blocks. You even lower it to a sequence of anonymous functions as inputs. The purpose of the do block is to shift a large first argument to the tail of a higher-order function call so we don’t have to scroll to see how many inputs it has. A sequence of large definitions like this would defeat that purpose, and a let block making some temporary input variables for the call would be far easier to read.

julia> let
         function f1(e)
             println("g", e)
         end
         function g1(o)
             println("f", o)
         end
         oe(f1, g1) # don't need to scroll to see its 2 inputs
       end
goo
fee
2 Likes

Indeed, readability is most important here. I was just thinking, well, if we can shift one large initial argument to the tail, why not do it for two or maybe even more. However, the latter may indeed turn out confusing and separate functions would serve better in this case. Thank you for your prompt reply!