What is begin...end in Erlang used for?
Asked Answered
I

4

16

I just stomped at a begin...end in Erlang's documentation (here), but it doesn't give some examples of how it is useful.

Looking here in StackOverflow I found two cases where people would be using begin...end, both in list comprehensions:

But I wonder if there are more of such uses.

Can anyone provide another scenario in which a begin...end is useful in Erlang?

Thanks

Inextricable answered 21/11, 2013 at 15:39 Comment(3)
you can just use it to create quick code to evaluate and create the value you want. e.g. T = {1,2,begin 1+2 end}. will evalute T={1,2,3}Spiny
@MuzaayaJoshua But your example works even without begin end. Can anyone give an example that actually requires using being end? Or are they like curly brackets?Blackwell
@Blackwell check the answers below, there are examples that cannot be accomplished without begin...endInextricable
A
12

Macros, for example:

-define(M(A, B),
    begin
        C = foo(),
        bar(A, B, C)
    end).
Antre answered 21/11, 2013 at 19:50 Comment(3)
Thanks @P_A. Some clarifications so others don't have to test what I just tested. A macro with more than one expression can be made without begin...end. But, if you are using that macro in a place where only one element is expected (like in io:format) then begin...end is the way to go.Inextricable
How is this better than -define(M(A, B), bar(A, B, foo())). ?Beshrew
It's just an example to show how begin...end can be usedAntre
P
9

To evaluate a catch (always the same idea to have multiple expression reduced to one)

Res = (catch
    begin
        C = foo(Bar),
        io:format("evaluation of C ok~n"),
        D = bar(A, B, C)
     end),
Pew answered 21/11, 2013 at 21:1 Comment(0)
L
5

As previous answerers mentioned, this construct is used whenever you need to have multiple expressions but only one is allowed.

However, the majority of such cases would be considered a stinky style. I can remember only a few of places where a single expression is expected: an argument in a function call, catch expression, case of, try of and list comprehension. All of them except for list comprehension shouldn't be used with begin end construct because the variables are leaking to the outer scope probably causing the subsequent bindings to become matches.

List comprehension expression is different because it is transformed to a separate function with its own scope and no variable introduced in begin end leaks to the outer scope.

Lavona answered 21/11, 2013 at 21:42 Comment(4)
In which part of try of ? in each of the 3 segments of a try...of...catch we can put several instructions.Inextricable
Hmm, you're right. I thought only one expression is allowed in try <here> of ... catch ... end expression. Actually this works fine: try a = b, b of A -> A catch C:E -> {C,E} end.. Thanks!Lavona
@Blackwell Wrong in what? The answer proposes the only valid usage of begin end is in list comprehensions. And as try of allows multiple expressions begin end has even less applications even if you don't care about the quality of the code.Lavona
@DmitryBelyaev In your answer you wrote "case of" takes a single expression, and MondKin said it does not. "case of" takes multiple true/false conditions as a single expression, but you cannot run an assignment statement for example before a true/false condition, see my answer below. What I wanted to say is that you are correct, however the definition of "expression" wasn't so clear.Blackwell
B
2

According to erlang documentation is it block expression that evaluates each expression but returns only the last one.

See this example (not using block expression):

A = 1,
case A + 1 of
    3 ->
        ok;
    _->
        nop
end.

% returns ok

Now you can define A within the case argument using block expression:

case begin A = 1, A + 1 end of
    3 ->
        ok;
    _->
        nop
end.

%returns ok

That evaluates A = 1, then returns the result of A + 1.

Now we know that this will not work:

case A = 1, A + 1 of
    3 ->
        ok;
    _->
        nop
end.

% returns syntax error before: ','
Blackwell answered 4/10, 2017 at 8:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.