Skip to content

Commit 3325761

Browse files
committed
Longer explanation on why only returning scalars
1 parent 4d2ade9 commit 3325761

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

ppcs/ppcTODO-more-list-utils.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ all { BLOCK } LIST
2929

3030
These operators are similar to `grep` in scalar context, though yield a simple boolean truth value relating to how many input values made the filter block yield true. `any` yields true when its block yields true for at least one of its input values, or false if they all yield false. `all` yields true only if every input value makes the block yield true, or false if at least one yields false.
3131

32-
These operators only yield a single scalar; in list context therefore they will just provide a single-element list containing that boolean scalar.
33-
3432
A consequence of these rules is what happens when given an empty list. A call to `any` with an empty list does not have any input values which made the block return true, so its result is false. Conversely, a call to `all` with an empty list does not have any input values which made the block return false, so its result is true.
3533

3634
The key difference between these operators and `grep` is that these will short-circuit and stop evaluating the block once its result is determined. In particular, the first time `any` sees a true result, it knows its result so it can stop; only testing more input values while each yields false. Conversely, `all` will stop as soon as it sees a false result, knowing that to be its answer; it only continues while each value yields true from the block. This short-circuiting is a key reason to choose these over the `grep` operator.
@@ -39,6 +37,17 @@ Additionally, because each operator returns a fixed boolean truth value, the cal
3937

4038
Like `grep`, each is a true operator, evaluating its block expression without an interposed function call frame. Thus any `caller` or `return` expression or similar within the block will directly affect the function containing the `any` or `all` expression itself.
4139

40+
These operators only yield a single scalar; in list context therefore they will just provide a single-element list containing that boolean scalar. This is so that there are no "surprises" if the operator is used in a list context, such as when building a key/value pair list for the constructor of an object. By returning a single false value even as a list, rather than an empty list, such constructions do no cause issues.
41+
42+
For example:
43+
44+
```
45+
Some::Class->new(
46+
option => (any { TEST } list, of, things),
47+
other => $parameter,
48+
);
49+
```
50+
4251
## Backwards Compatibility
4352

4453
As these new operators are guarded by named features, there are no immediate concerns with backward compatiblity in the short-term.

0 commit comments

Comments
 (0)