Monday, December 8, 2008

more on syntax

The more I think about it the more it becomes apparent that logically using square brackets for codeblocks/lambdas does NOT make sense for what I am trying to accomplish with let. Using { } for anon blocks and ( ) for lists as well as "grouping" and boolean statements makes much more sense thus saving square brackets for sub-statements and removing [ ] from arrays entirely as they can use dot notation or accessor methods. Consider:

$list = (1 2 3);
$list.each {$x | echo $x};

$number = let ($x = 10){
(add:{$y | $x += $y}
sub:{$y | $x -= $y});
#note if we wanted the method to be named add: we would do 'add:':{$y | $x += $y}
}
$number.add 10; #20
$number.sub 5; #15

#And from last time:
$decide = {$char | $char == a ? sum : mul};

$num = 10;
$letter = a;
$result = [$decide char:$letter] 6 6 ($num + 50);

Notice in the last one that this makes square brackets the equivelant of "apply" and keeps open brackets for grouping and or boolean operation which is what people coming from php would expect. The { } also feels more natural for anon code blocks for php-centric people as well.

Another approach would be using square brackets for "messages" sort of like obj c but more like sugar for dropping dots in objects i.e.

$range = (1..5);
[$list reverse each {echo _}]
#becomes
function block_name($arg0){echo $arg0;}
$list->reverse->each('block_name');

No comments: