Why do we need the `do` keyword?

We don’t need it, we want it.

@do f(y) x -> begin
...
end

is longer and less pretty than

f(y) do x
...
end

This is an operation we want to be pretty because it is really commonly used.
It basically takes the place of python’s with statements and more.
Basically this is a really common and useful pattern.
Some examples of the places I tend to use it:

open("file.txt") do fh
...
end


pmap(1:100) do x
...
end

mapreduce(hcat, 1:100) do x
 # return a vector
end

lock(lk) do
...
end

withenv(API_KEY="123423") do 
...
end

mktempdir() do dirname
...
end

sum(xs) do 
    ...
end

I wouldn’t like to make all those uglier.

At the end of the day, why do we do anything?
Since julia is just going to compile to machine code anyway.

Or less reductio ad absurdum: why allow anon functions?
Why not just have people write functions normally?
Or use a macro to create real functions out of lambdas syntax?

Looking at the history do blocks have been in the language since before 0.1 release.
Which is before my time.
The original reason is thus probably buried in a email chain somewhere.

22 Likes