Michael G Schwern <[EMAIL PROTECTED]> writes:

> Reading this in Apoc 4
>
>     sub mywhile ($keyword, &condition, &block) {
>         my $l = $keyword.label;
>         while (&condition()) {
>             &block();
>             CATCH {
>                 my $t = $!.tag;
>                 when X::Control::next { die if $t && $t ne $l); next }
>                 when X::Control::last { die if $t && $t ne $l); last }
>                 when X::Control::redo { die if $t && $t ne $l); redo }
>             }
>         }
>     }
>
> Implies to me:
>
>     A &foo prototype means you can have a bare block anywhere in the
>     arg list (unlike the perl5 syntax).
>
>     Calling &foo() does *not* effect the callstack, otherwise the
>     above would not properly emulate a while loop.
>
> If that's true, can pull off my custom iterators?
> http:[EMAIL PROTECTED]/msg08343.html
>
> Will this:
>
>     class File;
>     sub foreach ($file, &block) {
>         # yeah, I know.  The RFC was all about exceptions and I'm
>         # not using them in this example.
>         open(FILE, $file) || die $!;
>
>         while(<FILE>) {
>             &block();
>         }
>
>         close FILE;
>     }        

Hmm... making up some syntax on the fly. I sort of like the idea of
being able to do

    class File;
    sub foreach ($file, &block) is Control {
        # 'is Control' declares this as a control sub, which, amongst
        # other things 'hides' itself from caller. (We can currently 
        # do something like this already using Hooks::LexWrap type
        # tricks.

        open my $fh, $file or die $!; POST { close $fh }
        
        while (<FILE>) {
            my @ret = wantarray ?? list &block() :: (scalar &block());
            given $! {
                when c::RETURN { return wantarray ?? @ret :: @ret[0] }
            }
        }
    }

This is, of course, dependent on $! not being set to a RETURN control
'exception' in the case where we just fall off the end of the block.

It's also dependent on being able to get continuations from caller
(which would be *so* cool

> allow this:
>
>     File.foreach('/usr/dict/words') { print }

Sounds plausible to me.

> or would the prototype be (&file, &block)?

I prefer the ($file, &block) prototype.

> And would this:
>
>     my $caller = caller;
>     File.foreach('/usr/dict/words') { 
>         print $caller eq caller ? "ok" : "not ok" 
>     }
>
> be ok or not ok?  It has to be ok if mywhile is going to emulate a
> while loop.

In theory there's nothing to stop you writing it so that that is the
case. I'd like it to be as simple as adding an attribute to the
function declaration (and if it isn't that simple out of the box, it
will almost certainly be, if not trivial, at least possible to write
something to *make* it that simple...)


-- 
Piers

   "It is a truth universally acknowledged that a language in
    possession of a rich syntax must be in need of a rewrite."
         -- Jane Austen?

Reply via email to