Cascading ad-hoc Operators

Prev Next

Introduction

Unlike in other common programming languages, B4P supports cascaded ad-hoc operators. Cascading is realized by applying multiple ad-hoc operators in sequence which are then always calculated from left to right. Cascading may be used for both prefix and postfix usages.
Some examples:

++ ++ Increment twice
++ -- Makes no sense (original value)
-- -- Decrements twice
++(3) ++ Adds 4
++ ++(3) Adds 4
**(2) ++ Multiplies 2, adds 1
++ **(2) Syntax error. Scaling not allowed after ad-hoc incrementing or decrementing
**(2) **(3) This combination is OK. Multiples by 6
**(1/2) Divides by 2

  a[] = 10;
  b[] = date( "2020-05-01 15:00:00" );
  c[] = { { 1, 2 }, { 3, 4 }, 5 };

  a[] ++ ++ ++ -- ++;        // Becomes 13, even if code appears less meaningful
  b[] ++(3.75) ++;       // 4 days and 18 hours later
  c[] **(2) ++(2);       // Double all elements, then add 2 on all of them

  echo("a[] = ", a[],"  b[] = ", b[] );
  echo("c: ", c[] );
a[] = 13  b[] = 2020-05-06 09:00:00
c: {{4,6},{8,10},12}
Try it yourself: Open LAN_Features_cascading_ad-hoc_operators.b4p in B4P_Examples.zip. Decompress before use.