Documentation for infix notandthen

Documentation for infix notandthen, assembled from the following types:

language documentation Operators

From Operators

(Operators) infix notandthen

Returns Empty upon encountering the first defined argument, otherwise the last argument. Last argument is returned as-is, without being checked for definedness at all. Short-circuits. The result of the left side is bound to $_ for the right side, or passed as arguments if the right side is a Callable, whose count must be 0 or 1.

At first glance, notandthen might appear to be the same thing as orelse operator. The difference is subtle: notandthen returns Empty when it encounters a defined item (that isn't the last item), whereas orelse returns that item. In other words, notandthen is a means to act when items aren't defined, whereas orelse is a means to obtain the first defined item:

    sub all-sensors-down     { [notandthen|@_True             }
    sub first-working-sensor { [orelse]     |@_'default sensor' }
    all-sensors-down NilNilNil
      and say 'OMG! All sensors are down!'# OUTPUT:«OMG! All sensors are down!␤» 
    say first-working-sensor NilNilNil# OUTPUT:«default sensor␤» 
    all-sensors-down Nil42Nil
      and say 'OMG! All sensors are down!'# No output 
    say first-working-sensor Nil42Nil;  # OUTPUT:«42␤» 

The andthen operator is a close relative of without statement modifier, and some compilers compile without to notandthen, meaning these two lines have equivalent behaviour:

    sub good-things { fail }
    'boo'.say without good-things;
    good-things() notandthen 'boo'.say;