--- Austin Hastings <[EMAIL PROTECTED]> wrote: > --- Brent Dax <[EMAIL PROTECTED]> wrote:
> > Except that none of the other ones exist in Perl 6. :^) > Thinking about this some more, and considering the desirability of lazy evaluation, I think incremental execution might be the right way. (Also, at the bottom is a weird version of switch/case that's not separable. But I don't know if it could work.) Here's more tries, with _ --- still assumed as separable-sub continuation mark is separable --- declaration of separable-sub is incremental --- declares that sep-sub bits are called piecemeal is optional --- this part of sep-sub doesn't have to appear is repeatable --- this part of sep-sub can appear more than once is floating --- this part can mingle with other floaters also, with defaults being not optional, not repeatable, not floating. also, borrowing the convert_to_sub($) function that DC wrote a while ago. # (Modulo stuff to pass-inward the outer topic) # (Modulo also stuff to pass-outward any leave ops.) sub do_ife($block) { my &branch := convert_to_sub($block); branch(); } Keyword-like version of 'if_elsif_elsunless_else': sub if ($c, $block) is separable is incremental is given($c) { when true { do_ife($block); leave; } } _ elsif ($c_, $block) is optional is repeatable is floating is given($c) { when true { do_ife($block); leave; } } _ elsunless ($c, $block) is optional is repeatable is floating is given($c) { when false { do_ife($block); leave; } } _ else ($block) is optional { do_ife($block); } Enabling: if ($a == 'a') { print 1; } elsif ($a == 'b') { print 2; } elsunless ($a < 'z') { print "out of range"; } elsif ($a == 'c') { print 4; } elsif ($a == 'd') { print 8; } else { print 0; } Operator-like version of 'if_otherwise': sub infix:if($block, $c) is separable is repeatable is incremental is given($c) { when true { do_ife($block); leave; } } _ otherwise($block) is optional # is fixed { do_ife($block); } Which gives us: moo() if cond goo() if con2 gai() if con3 pan() otherwise; # "elsewise", for those west of the Pecos... ================= Here's the Java-like try/catch(e)/catch/finally: sub try($b) is separable is incremental { my $bogus = sub { return 1; } # Need a ref the user can't throw. my $exc = $bogus; my &block := convert_to_sub($b); block(); CATCH { if ($exc == $bogus) { $exc = $!; } else die; } # I'd like to leave(), but there might be a finally { } block, # below. } _ catch($e, $block) is optional is repeatable { if ($exc ~~ $e) { $exc = $bogus; do_ife($block); } } _ catch($block) is optional { if ($exc != $bogus) { $exc = $bogus; do_ife($block); } } _ finally($block) is optional { $exc = undef; # Not $bogus. Throw in here goes through. do_ife($block); } ================ Here's perl5-like do-with-optional-while sub do($b) is separable is incremental { my &block = convert_to_sub($b); block(); } _ while($c) is optional { while ($c) { block(); } # Note: this is use of "other" while. } ================= Pascally repeat/until using do/while. This one doesn't get called until the end. sub repeat($b) is separable # NOT incremental _ until($c) { do $b while (!($c)); } ================= Here's switch/case, NOT using separable-sub syntax. Instead, I think a "global method" gets it. # Help! I'm unsure if the colon-syntax works right, here. sub switch($c, &cases) { my $fallthru = 0; given $c { cases(); } } #global method. Is this possible? Or should it be Scalar::? # The idea is to allow case x: {y} as object-indirect syntax # for x.case({y}) -- I need to define a "global" .case method. method case($c, &block) is given($_) { if ($fallthru) { block(); } else { when $c { block(); # A "leave switch" in block() skips this part. $fallthru = 1; } } } Allowing: switch ($x) { case 1: { print "Fall"; } case 2: { print "Through"; leave switch; } case 10: { print "Hello,world"; leave switch; } case 20: { print "Too much."; leave switch; } }