On 11/16/05, Luke Palmer <[EMAIL PROTECTED]> wrote:
> Here is some perplexing behavior:
>
>     say "Foo";
>     hello there;
>
>     sub hello () {
>         say "Bar";
>     }
>
>     sub there () {
>         say "Baz";
>     }
>
> This prints:
>
>     Foo
>     *** No compatible subroutine found: "&hello"
>         at lazy.p6 line 2, column 1-12
>
> I would expect it to print:
>
>     Foo
>     Baz
>     *** No compatible subroutine found: "&hello"
>         at lazy.p6 line 2, column 1-12

Okay, this makes sense.  Apparently Pugs supports "is lazy" (cool!). 
So you don't know when to evaluate your arguments until after you've
selected the call.

There are two reasons I've posted to perl6-language this time.  First
of all, is this acceptable behavior?  Is it okay to die before the
arguments to an undefined sub are evaluated?

Second, consider this "is lazy" code:

    sub foo ($bar is lazy) {
        my $bref = \$bar;
        do_something($bref);
    }
    foo(42);

This will evaluate $bar, even if it is not used in do_something.  In
fact, this will evaluate $bar even if the do_something call is omitted
altogether.  This doesn't give you much control over the time of
evaluation, and presumably if you're saying "is lazy", control is
precisely what you want.

I think we need more control.  I think "is lazy" parameters should
pass a thunk that needs to be call()ed:

    sub foo ($bar is lazy) {
        say $bar; # says something like Thunk(...)
        say $bar();  # evaluates parameter and prints it
        say $bar; # still says something like Thunk(...)
        say $bar();  # doesn't evaluate again, just fetches
    }

Whaddaya think?

Luke

Reply via email to