[erlang-questions] Newbie: Is there a short-circuit for the comma operator?

Raimo Niskanen <>
Wed Oct 10 08:53:16 CEST 2007


Nope. Such a thing does not exist.

But it seems you want to do something that is well suited for throw/1.

try begin Exp1, Exp2, Exp3, Exp4 end of
   V -> V % Return value from Exp4
catch X ->
   X % Value from embedded throw/1
end

If any of ExpN evaluates throw(done),
the catch clause is evaluated with X bound to 'done'.

See also the Erts Reference manual.



On Wed, Oct 10, 2007 at 07:14:11PM +1300, James Cone wrote:
> Hello All,
> 
> This isn't explicit in the Erlang Reference Manual (v5.5.5), so you may 
> not think of it this way:
> 
> There is (something morally equivalent to) an operator ",", which is the 
> lowest priority of all operators, and the evaluation of
>     exp1, exp2
> is:
>    - have all of the side-effects of exp1
>    - have all of the side-effects of exp2
>    - return the value of exp2
> 
> eg
> 
> 15> 0, 1.
> 1
> 16>
> 
> 
> Is there an operator or function that short-circuits the comma operator, 
> so if exp1 uses it, exp2 will not be evaluated?
> 
> eg (supposing it's called return, which is the name of the equivalent 
> operator in C)
> 
>  > if 1==0 -> -1; true -> return 0 end, 1.
> 1
>  >
> 
> I want this because I have a function with some deeply nested tests, and 
> I want to tail-recurse from only some of the cases, without copying all 
> of the following expressions (after a comma operator) into all of the 
> other cases.
> 
> Am I making sense?  Does such a thing exist?
> 
> Regards,
> James.
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list