Ad-hoc Operators

Prev Next

Introduction

Ad-hoc operators are features which apply an immediate action on the variable or table field referenced. The increment and decrement operators (++ and --) common in C/C++, Java and Python are just two of a variety of ad-hoc operators. Ad-hoc operators can be applied in following ways:

  • Stand-alone use
    • As prefix: ++a[];
    • As postfix: a[]--;
  • As part of a left-hand expression of assignments
    • As postfix: a[]++ = 1; (Prefix use would not make sense as it increments the value before being overwritten.
  • As part of a right-hand expression of
    • As prefix: b[] = 3 + ++a[];
    • As postfix: b[] = 3 + a[]++;
    • Combination of prefix and postfix use is allowed, e.g. ++a[]--. Actually, (a[]+1) in an expression is more meaningful.
    • In parameters for function calls, e.g. echo( ++a[] )

++ Increment
-- Decrement
++(n) Increment by specified numeral or numeric expression (negative values will obviously decrement the target value)
--(n) Decrement by specified numeral or numeric expression (negative values will obviously increment the target value)
**(n) Apply scaling factor, with numeral or numeric expression in parentheses. An equivalent symbol for division does not exist. Use a reciprocal value in combination with the scaling factor instead.

Note: The two consecutive characters need to be written together without spaces. Otherwise they are recognized as plus, minus and multiplication symbols. False example: c[] = - - a[]; where the value of a[] is negated twice and assigned to c[]. Ad-hoc operators are allowed on target value with following types:

  • Numerals
  • Blank strings, which are treated as 0
  • Non-blank strings: All ad-hoc operations will be ignored. No error messages occur, which may be beneficial when processing tables with mix of numbers and text
  • Dates:
    • If containing dates: +/-1 represents next/previous days
    • If containing times: For example Adding a fraction of 1, e.g. **(0.25) for 6 hous, is possible
  • Parameter sets: All elements, including nested ones, are affected
  • Booleans: ++ sets to true -- sets to false.

Ad-hoc operators can be applied on variable as well as table cells. However following restrictions apply:

  • Applying ad-hoc operators on unknown variables.
    • Exception to rule: Post-fix use in assignments, e.g. b[]++ = 3; because the incrementation is carried out after the assignemnt is completed.
  • Variables where specific protections apply (read only)
  • Ad-hoc operators are not allowed in transactions, e.g. a[]++ <== b[];
  • They are also not allowed in function call requiring variables as code pices, e.g. in dim(), protect(), etc.
  • Ad-hoc operatoros on multiple table cells (e.g. in horizontal table access). Only single cells are allowed.

B4P provides following ad-hoc operators;

Ad-hoc operators can also be cascaded, e.g. a[]++ ++, a[] **(3) ++. Following restriction applies: All scaling operations must happen before ++ and/or -- operators. Not valid: a[] ++ **(3). However ++a[]**(3) is OK.

  a[] = 10;
  b[] = 20;
  d[] = 30;

  echo("Demonstrate ad-hoc operations in right-hand expressions:");

  //    10          11            9             11
  echo( a[]++, ' ', a[]--, ' ', --a[], ' ', ++ ++a[] );

  //    22             22 -> 66               6
  echo( **(2)a[], ' ', a[]**(3), ' ', **(1/11)a[] );

  x[] = --(2) a[]++;  // Decrement a by 2, assign, then increment by 1. Result: 4, 5

  echo( x[], ' ', a[]);

  echo(new line, "Demonstrate ad-hoc operations in left-hand expressions:");

  e[]++ = b[];       // becomes 21
  f[]**(2) = 10;     // becomes 20

  echo( e[], ' ', f[]) ;

  echo(new line, "Demonstrate stand-alone ad-hoc operations:");

  d[]++;             // Simple one, 31.
  **(2)e[]--;        // Double 21 to 42, subract by 1 becomes 41.

  echo( d[], ' ', e[]) ;

  echo(new line, "Demonstrate stand-alone ad-hoc operations in tables:");

  table create ( t );    // Ad-hoc operators applied on table cells

  [t:0,0]++;             // Table entry was blank. Arithmetics interpret blanks as zero;  Contains 1.
  [t:0,0]**(3);          // Multiplied by 3.
  echo( [t:0,0] );       // 3

  echo(new line, "Demonstrate stand-alone ad-hoc operations with dates:");

  d[] = date( "2020-08-01 18:30:00" );
  echo( --d[] ); // Previous day
  echo( ++(0.5)d[] ); // Next day, 06:30

  echo(new line, "Demonstrate stand-alone ad-hoc operations with parameter sets:");

  a[] = { 1, 2, 5, 10, { 20, 50 } };
  a[]++;
  echo(a[]);
Demonstrate ad-hoc operations in right-hand expressions:
10 11 9 11
22 22 6
4 5

Demonstrate ad-hoc operations in left-hand expressions:
21 20

Demonstrate stand-alone ad-hoc operations:
31 41

Demonstrate stand-alone ad-hoc operations in tables:
3

Demonstrate stand-alone ad-hoc operations with dates:
2020-07-31 18:30:00
2020-08-01 06:30:00

Demonstrate stand-alone ad-hoc operations with parameter sets:
{2,3,6,11,{21,51}}
Try it yourself: Open LAN_Features_ad-hoc_operators.b4p in B4P_Examples.zip. Decompress before use.

See also

Ad-hoc operators overview