On Tue, Aug 23, 2005 at 11:03:52AM +0300, Yuval Kogman wrote:
: On Mon, Aug 22, 2005 at 13:25:57 -0700, Larry Wall wrote:
: > On Tue, Aug 23, 2005 at 04:09:29AM +0800, Yiyi Hu wrote:
: > : my( $s, $t ); $s = "value t is $t"; $t = "xyz"; print $s;
: > : in perl 5, it will give a warning, and won't do "right" thing.
: > : we have to use other way or eval '$s' before print to get a "correct" 
answer.
: > : 
: > : So I wonder, If we can make $scalar lazy also. As array now is lazy by 
default.
: > : 
: > : Even if making scalar lazy might cause problem sometimes, Is it
: > : possible to add a property which is like
: > : my $var is lazy; to handle these situation?
: > 
: > In Perl 6 you make lazy scalars by putting curlies around them:
: > 
: >     my( $s, $t ); $s = { "value t is $t" }; $t = "xyz"; print $s();
: > 
: > Currently we also require the de-lazifying context to supply a
: > postfix .() marker, but possibly that could be assumed in a string
: > or numeric context.
: > 
: > I really don't see much benefit in making it easier than that.
: 
: I see one:
: 
:               class Object {
:                       has $.expensive_to_compute_but_cachable = delay {
:                               ...
:                       };
:               }

Which already seems to be there with

    lazy {...}

which is, I presume, mostly syntactic sugar for something like:

    sub is cached {...}

along with the notion of implicit .() on "value" use, as I already
conceded above.  Plus we can assume "is cached" on any function
that can be proven "pure".  So I think maybe you're responding to a
different position than the one I'm actually taking.

: OR
: 
:               class Object {
:                       has $.expensive_to_compute_but_cachable is delayed = 
...;
:               }
: 
: And no one has to know that it's lazy.
: 
: With explicit code refs, you need to make an accessor:
: 
:               class Object {
:                       has $.expensive_to_compute_but_cachable;
: 
:                       method expensive_to_compute_but_cachable (
:                               $.expensive_to_compute_but_cachable # i forget 
how
:                               # autrijus's "returned" attr on params works
:                       ) {
:                               $.expensive_to_compute_but_cachable //= ...;
:                       }
:               }
: 
: Which is just as much headache that we had to do in perl 5.

We could probably extend "is cached" to attributes (and their implied
accessors) if lazy blocks aren't sufficient.

: Also, lazifying semantics are consistent with
: 
:       sub &infix:<||> ($left, $right is delayed) {
:               $left ?? $left :: ** $right; # can you steamroll a scalar?
:       }

I'd really like to discourage people from writing thunks without curlies.

I don't think steamrollering a scalar works--it would just take it
as an operation on a list rather than on the elements of the list.
You can't really delazify until you know what kind of a scalar you
want, I suspect, because the lazy closure itself might need to know
in what context to run.  Otherwise you just have to return a proxy
object that might turn into a string or number or int or whatever
value is eventually wanted.  Of course, presumably infix:<||> has
that context already, so maybe we're looking for something like

    $right.want

to force value-finding behavior iff it's called for.  After all, you
probably want it to stay lazy if you call it in undifferentiated scalar
context:

    $x = $y || lazy { z() }

But maybe I'm confusing levels there.  Wouldn't be the first time...

Larry

Reply via email to