develooper Front page | perl.perl6.language | Postings from August 2001

Re: properties, revisited

Thread Previous | Thread Next
From:
Damian Conway
Date:
August 7, 2001 15:26
Subject:
Re: properties, revisited
Message ID:
200108072225.IAA02993@indy05.csse.monash.edu.au
   > More questions regarding the new 'given when' construct.

More answers (modulo Larry :-)

   > 1) Is a 'when' clause required?  Is it:
   >
   > [ LABEL: ] given ( expr_1 ) {
   >            when expr_2 : block
   >            [ when expr_3 : block ...]
   >            expr_n [;]
   >             }
   >
   > or
   >
   > [ LABEL: ] given ( expr_1 ) {
   >            [ when expr_2 : block ...]
   >            expr_n [;]
   >             }

Not required. It's:

        [ LABEL: ] given ( expr_1 ) {
                [statement ; ...]
                [statement]
        }

A C<when> statement is just a particular kind of statement.


   > 2) In expressions 2 through n-1 - the equivalent to 'case' - you
   >    may refer to the return value of expression 1 with $^_. Does
   >    this imply that expression 1 will always (and only) be evaluated
   >    in scalar context? If not, can you mix contexts across 'when'
   >    clauses? Is the value of $^_ then a reference to the return
   >    value?

The C<given> expression is evaluated in scalar context.


   > 3) Can you refer to $^_ within expression n - the equivalent to 'default:'?
   > given ( a() ) {
   >    print $^_;   # No when clause?  See #1
   > }

$^_ isn't an alias for the C<given>'s expression.
It's a higher-order function placeholder
(see: http://www.yetanother.org/damian/Perl5+i/higherorder.html)

So the answer is "no".


   > 4) Can you refer to $^_ within the code blocks of each 'when' clause?
   > given ( a() ) {
   >     when $^_ == 1 : { foo($^_) }
   >     when $^_ == 2 : { bar( 5 + $^_ ) }
   >     when $^_ == 3 : { ++$^_ and print $^_ }  # Mutable? See #5
   > }

No. See the previous answer.

If the gvien value is needed within the block, the most likely mechanism
would be:

     given ( my $a = a() ) {
         when $^_ == 1 : { foo($a) }
         when $^_ == 2 : { bar( 5 + $a ) }
         when $^_ == 3 : { ++$a and print $a }  # Mutable? See #5
     }


   > 5) Is $^_ an alias, reference (See #2), or a copy of the return value of
   > expression 1, and is it mutable?  Does it have the appropriate magic and
   > overloading capabilities?

None of the above. See above.


   > 6) How many times is expression 1 evaluated?

Once.


   > $a = 0;
   > given ( ++$a ) {
   >     when $^_ > 5 : { #foo }
   >     when $^_ > 4 : { #bar }
   >     when $^_ > 3 : { #baz }
   >     #foobar
   > }
   >
   > I would expect that $a would equal '1' after #foobar executed.  But if
   > multiple contexts are allowed (see question 2), how would

N/A.


   > 7) The 'when' blocks are not fall-through, a la C's switch statement.  You
   > can obtain a similar result by invoking 'next' within the 'when' block.
   >
   > Is this regardless of whether expr2 would have also evaluted true?  (IOW,
   > does it truly jump to the next block?)  If not, does it jump to the
   > expression after that (if it exists) and try again, or does it default
   > expression (since the original next expression evaluated false)?

A C<next> within a C<when>'s block causes control to jump to the next
statement in the innermost surrounding <given>'s block.
If that is a C<when> statement, the C<when> test is then evaluated as normal.


   > 8) Is the order of evaluation guaranteed?  (I assume so, but just thought
   > I'd check.)

I certainly hope so! ;-)


   > 9) It has a lexical construct similar to a conditional block, so that
   > lexical variables declared in the 'given' expression are within the inner
   > scope, correct?

Correct.


   > 10) I've already asked Damian this question on the side, but I'll
   >     repeat (and expand) it here, to give me an even ten. Should it
   >     be a single default expression, multiple default expressions,
   >     or a default block?

Any of the above. A C<given> block is just a block. A C<when> statement
is just a statement. You can put any statement inside any block, so you
can put both C<when> and non-C<when> statements inside a C<given> (in any
sequence and multiplicity you like).

Damian


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