Skip to content

Commit

Permalink
Merge pull request #59 from rabbiveesh/veesh/moar-short-circuit
Browse files Browse the repository at this point in the history
doc: be explicit about the short-circuiting behavior
  • Loading branch information
book authored Nov 24, 2024
2 parents 0b419bb + 6e9684b commit 20ab419
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions ppcs/ppc0021-optional-chaining-operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@ is equivalent to:

with the important caveat that EXPR1 is only evaluated once.

When used in a longer chain of dereferences, an undef value will short-circuit the entire
chain, rather than just a single expression:

EXPR1 ?-> EXPR2 ?-> EXPR3

is equivalent to:

```perl
if (defined EXPR1) {
if (defined EXPR1->EXPR2) {
return EXPR1->EXPR2->EXPR3
} else {
return ()
}
} else {
return () # empty list
}
```

## Backwards Compatibility

All code with `?->` currently yields a compile time syntax error, so there
Expand Down Expand Up @@ -170,11 +189,8 @@ Expected common uses:
# my $class = 'SomeClass'; $class->new if defined $class;
my $class = 'SomeClass'; $class?->new;

# my $obj = %SomeClass:: ? SomeClass->new : ();
my $obj = SomeClass?->new; # TBD: see 'Future Scope' below.

# my @objs = (%NotValid:: ? NotValid->new : (), %Valid:: ? Valid->new : ());
my @objs = ( NotValid?->new, Valid?->new ); # @objs == ( ValidObject )
# defined $aref ? $aref->[0] = 9001 : ()
$aref?->[0] = 9001; # $aref remains undef in the undef case
```

Unusual and edge cases, for comprehension:
Expand Down

0 comments on commit 20ab419

Please sign in to comment.