How to avoid short-circuit evaluation on
Try this: [model1, model2].map(&:valid?).all? It’ll return true if both are valid, and create the errors on both instances.
Try this: [model1, model2].map(&:valid?).all? It’ll return true if both are valid, and create the errors on both instances.
To delve into the past: Originally in guards there were only , separated tests which were evaluated from left-to-right until either there were no more and the guard succeeded or a test failed and the guard as a whole failed. Later ; was added to allow alternate guards in the same clause. If guards evaluate … Read more
There’s a subtle difference, because anyMatch family uses a predicate, while findAny family does not. Technically findAny() looks like anyMatch(x -> true) and anyMatch(pred) looks like filter(pred).findAny(). So here we have another issue. Consider we have a simple infinite stream: Stream<Integer> s = Stream.generate(() -> 1); So it’s true that applying findAny() to such stream … Read more
Yes, it’s guaranteed, otherwise such operators would lose much of their usefulness. Important notice: this is valid only for the builtin && and ||; if some criminal overloads them, they are treated as “regular” overloaded binary operators, so in this case both operands are always evaluated, and in unspecified order as usual. For this reason, … Read more
Due to the low precedence of the ‘or’ operator, or3 parses as follows: sub or3 { my ($a,$b) = @_; (return $a) or $b; } The usual advice is to only use the ‘or’ operator for control flow: @info = stat($file) or die; For more discussion, see the perl manual: http://perldoc.perl.org/perlop.html#Logical-or-and-Exclusive-Or
Since you wanted the spec, here it is (from §15.25 Conditional Operator ? :, the last sentence of the section): The operand expression not chosen is not evaluated for that particular evaluation of the conditional expression.
The C# specification guarantees that both sides are evaluated exactly once from left-to-right and that no short-circuiting occurs. 5.3.3.21 General rules for expressions with embedded expressions The following rules apply to these kinds of expressions: parenthesized expressions (§7.6.3), element access expressions (§7.6.6), base access expressions with indexing (§7.6.8), increment and decrement expressions (§7.6.9, §7.7.5), cast … Read more
Short-circuiting is where an expression is stopped being evaluated as soon as its outcome is determined. So for instance: if (a == b || c == d || e == f) { // Do something } If a == b is true, then c == d and e == f are never evaluated at all, … Read more
Yes, it says so in the C# Language Specification (highlighting by me): A null coalescing expression of the form a ?? b requires a to be of a nullable type or reference type. If a is non-null, the result of a ?? b is a; otherwise, the result is b. The operation evaluates b only … Read more
Reasons NOT to use short-circuit evaluation: Because it will behave differently and produce different results if your functions, property Gets or operator methods have side-effects. And this may conflict with: A) Language Standards, B) previous versions of your language, or C) the default assumptions of your languages typical users. These are the reasons that VB … Read more