Why compound expressions?

One use for these blocks is in comprehensions:

A = [ begin
           x = y^2
           x - x^2 + 2
         end
         for y = 1:5 ]

You could make a function and use it inside the comprehension instead, but sometimes this is convenient. It’s used whenever you want to use a multiline block of code somewhere, eg to pass as an argument to a macro (which is very commonly used for @testset from the Test standard library).


To generalize what everyone else has said: blocks allow you to convert a list of statements (syntactic "phrases" that have no values, i.e., can't be assigned) to one expression (a "phrase" that represents values and can be assigned).

For example, while you shouldn't, you can write

x = begin
    s = 0
    for i = 1:10
        s += i^2
    end
    s
end

to assign x to the result of a looping operation. (With the restriction that the last statement in the sequence must actually be an expression -- otherwise, you would have no value for the expression.)

A legitimate use case of this is generated code. For example, you might turn

x = @somthing bla blub

into

x = begin
   (stuff involving bla and blub)
end

transparently to the user, while the @something can freely generate any language constructs.

Or if you want to write an anonymous function with a longer body (and not use the function form):

f = x -> begin
   ...
end

Tags:

Julia