develooper Front page | perl.perl5.porters | Postings from March 2007

Re: Differences between state variables and smart matches in 5.10 and 6

Thread Previous | Thread Next
From:
David Nicol
Date:
March 26, 2007 10:31
Subject:
Re: Differences between state variables and smart matches in 5.10 and 6
Message ID:
934f64a20703261030g5874b4b9kc352709d26602109@mail.gmail.com
On 3/26/07, Rafael Garcia-Suarez <rgarciasuarez@gmail.com> wrote:
> On 26/03/07, Nicholas Clark <nick@ccl4.org> wrote:

> > then the RHS would only be evaluated if $a needed assigning to, and also the
> > assign op wouldn't need the flags any more.
>
> That's an interesting idea, although it needs a new op "state check"
> and heavy manipulation of the optree in the ck_ routines.

a ONCE framework would work as well; like a generalization of the /o
regex modifier.
The optree manipulation would be handled by the ONCE framework.  the /o system
might get refactored to use it as well.

opONCE(caller, block, next) when run would run block, set caller->next
to its next,
and then go there.  Implementing state variables with this proposed
tool would be
trivial.  Presuming that the RHS of their assignment expressions only
gets invoked
the first time through.  See below.

I find the semantics of executing the RHS but throwing out the result
really really
weird.  But on reflection understandable due to the question of what
is the extent
of the once block (whether a real once operator, or faked with flags)

      state $z++;  # compile-time warning?

 perl -lwe 'sub z(){my $z if 0; $z++}; print z,z,z'
012

 perl -lwe 'sub z(){my $z++ if 0; $z}; print z,z,z'
Use of uninitialized value in print at -e line 1.
Use of uninitialized value in print at -e line 1.
Use of uninitialized value in print at -e line 1.

 perl -lwe 'sub z(){my $z++ ; $z}; print z,z,z'
111

there are two competing models for what state should do:  one is having
C<state ... ;> map directly to C<my ... if 0;>  ; the other is the
static keyword
in C.  (right?)  And with flags and initializations, we're currently
doing something like

C<state ... = ...>  becomes C<my $1 if 0;  onlyonce($1) ? ($1 = $2) : ($2)>

with the repeated running of the thing in the initalizer slot striking
some (such as NC)
as odd.

      (state $x = 42)++;  #how should this work?

here's a succinct reference for the static keyword in C++:
http://www.phim.unibe.ch/comp_doc/c_manual/CPLUSPLUS/SYNTAX/static.htm

so, if C<state> is allowed as part of an l-value expression, Abigail's
term would expand to

    my $x if 0;
    ONCE { $x = 42 };
    $x++;

Is the big block on implementing ONCE that there is no previous
pointer in an optree node?
That can be solved by remembering what the last node was, at running time.


Of course we could throw away the last half-year's work and just
document C<my ... if 0> as
a recommended practice instead of a deprecated hack :) (/me ducks)

if ONCE had an optional suffice HENCE which would mean replace the
once-body with
the hence-body instead of simply skipping to the following op, that
would be cool.  (does
the p6 ONCE have such a feature?)

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About