~

Posted on Wed, 25th January 2006 at 12:27 under Software, Eureka!

The may be operator. A syntax for expressing a conditional cause chain or known sequence of expressions likely to resolve to a single value. Akin to the SQL function COALESCE().

PHP example

Expression without may be operator:

if( !isset( $id ) )
   if( isset( $_COOKIE['id'] ) )
      $id = $_COOKIE['id'];
   else
      if( isset( $_POST['id'] ) )
         $id = $_POST['id'];
      else
         if( isset( $_GET['id'] ) )
            $id = $_GET['id'];
         else
            $id = -1;

Expression with may be operator:

$id ~ $_COOKIE['id'] ~ $_POST['id'] ~ $_GET['id'] ~ -1;

Or, if you want to be fancy, declare a dimension (thanks Larry):

$id ~_['id']~ $_COOKIE ~ $_POST ~ $_GET ~ -1;

And, if Larry is reading, another tribute to him, a functional dimension:

$id ~is_numeric(_['id'])~ $_COOKIE ~ $_POST ~ $_GET ~ -1;

Enigmatic? Me?

3 Responses

  1. This isn’t an original idea. I’m sure I’ve borrowed the concept from another language somewhere, but memory fails as to which one.

    Reply
  2. I’m beginning to explore, once again, the possibilities afforded by interpreted over compiled languages. The ~ operator is little more than a pattern-based code generator, My functional dimension above raises an ‘orrible ambiguity - does $id become the result of the is_numeric() function or the value passed? In the case above, the answer has to be the return value of the function. What I really want is the value passed to the function, if the function succeeds with that value.

    $id ~_['id']?is_numeric(_)~ $_COOKIE ~ $_POST ~ $_GET ~ -1;
    

    Not quite so easy to read now, but expresses the conditional nature of the materialisation and the dimension.

    Reply
  3. Would $ be better than _ as the placeholder in the dimensional syntax?

    $id ~ $['id'] ? is_numeric($) ~ $_COOKIE ~ $_POST ~ $_GET ~ -1;
    
    Reply

Leave a Reply

You may also log in to post a comment.

XHTML:

If you want to <q>tag</q>, please balance these; a, i, em, b, strong, u, blockquote, q, ul, li, ol, abbr, code, pre, sub and sup.