in reply to Re^4: Why is the execution order of subexpressions undefined?
in thread Why is the execution order of subexpressions undefined?
First of all, not defining the order in all cases doesn't mean no order should be defined at all. && and || would not be as useful as they are now if their order of evaluation (first left operand, then right operand) wasn't defined.
Secondly, order of evaluation of (sub)expressions only matters if the expressions have side effects, and the side effects influence the value of the other sub expressions. But if you have:
it really doesn't matter whether fib(3) or fac(4) is evaluated first.sub fib {my $n = shift; $n <= 1 ? $n : $n + fib($n - 1)} sub fac {my $n = shift; $n <= 1 ? 1 : $n * fac($n - 1)} my $var = fib(3) + fac(4);
Were it written down exactly when evaluation order could affect the outcome of a statement, then it would be easy to avoid it--but it isn't.Huh? I don't get that. How is it easier to write an expression that isn't depending on the order of evaluation if the order of evaluation is defined? I mean, if you have:
then it's easier to avoid dependency on the order of evaluation if you know + causes its left hand side to be evaluated before its right hand side?my $i = 1; sub f {$i += 1; $_[0] * $i} print f(2) + f(3);
Unless you're advocating never using compound statements?Not at all. Just don't write expressions which use operators whose order of evaluation isn't defined, but whose outcome does depend on the order of evaluation. For instance, my previous example could have been written as:
and now it doesn't matter which order + has.my $tmp = f(2); print $tmp + f(3);
Indeed, if autothreading (in the threads sense rather than the P6 sense (if they are not the same thing?)), is ever going to be possible, it would be necessary to define the order of execution if the outcome of a compound statement is ever going to be predictable.So, you need order of execution to do things in parallel, but because there's an order of execution, two things can't happen in parallel?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
| A reply falls below the community's threshold of quality. You may see it by logging in. |