What is begin…end in Erlang used for?

后端 未结 4 712
你的背包
你的背包 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:38

    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: ','
    

提交回复
热议问题