~
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?
Libertus said: January 25th, 2006 at 13:08
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.
ReplyLibertus said: January 25th, 2006 at 17:56
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 . What I really want is the value passed to the function, if the function succeeds with that value.
Not quite so easy to read now, but expresses the conditional nature of the materialisation and the dimension.
ReplyLibertus said: January 25th, 2006 at 18:00
Would $ be better than _ as the placeholder in the dimensional syntax?
Reply