What is begin…end in Erlang used for?

后端 未结 4 724
你的背包
你的背包 2021-02-19 11:57

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 f

4条回答
  •  天涯浪人
    2021-02-19 12:56

    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.

提交回复
热议问题