Re: S04-related closure question

2008-09-22 Thread Patrick R. Michaud
On Sat, Jul 12, 2008 at 09:52:34PM -0500, Patrick R. Michaud wrote:
> What would be the expected output from the following?
> 
> my $a = foo();
> my $b;
> 
> {
> my $x = 1;
> sub get_x() { return $x; }
> sub foo()   { return &get_x; }
> $b = foo();
> }
> 
> my $c = foo();
> 
> say "a: ", $a();
> say "b: ", $b();
> say "c: ", $c();

If I'm reading the current version of S04 correctly,
I'm guessing the above will produce

   a: Use of undefined value
   b: 1
   c: 1

> As a followup question, what about...?
> 
> my @array;
> for 1..3 -> $x {
> sub get_x() { return $x; }
> push @array, &get_x;
> }
> 
> for @array -> $f { say $f(); }

This one would output  "1\n2\n3\n".

Are my interpretations correct here?

Pm


Re: Subroutine parameter with trait and default.

2008-09-22 Thread Michael G Schwern
Patrick R. Michaud wrote:
> On Sun, Sep 21, 2008 at 07:02:37PM -0700, Michael G Schwern wrote:
>> I'm pondering what the proper syntax is for a subroutine parameter with both 
>> a
>> trait and a default.  That is...
>>  sub foo ($arg = 42)
>> and
>>  sub foo ($arg is readonly)
>> together in one parameter.  Would that be
>>  sub foo ($arg = 42 is readonly)
>> or
>>  sub foo ($arg is readonly = 42)
>>
>> The first looks ambiguous, what if the trait is meant to apply to the 
>> default?
>> The second looks downright wrong.
> 
> 
> The STD.pm grammar [1] shows that the second is the correct form -- 
> i.e., default values occur after traits.  (See "token parameter" 
> on or about line 2630.)  I think part of the reason for this is that
> traits appearing after the default value would be applied to the
> default value instead of the parameter.  A few other examples from
> the synopses seem to confirm this pattern:
> 
>   S03:2289:my Dog $fido is trained is vicious = 1
>   S03:3828:constant Dog $foo is woof = 123;
>   S06:1558:constant $pi is approximated = 3;
>   S12:1393:has SomeType $.prop is rw = 1;
> 
> 1.  http://svn.pugscode.org/pugs/src/perl6/STD.pm

Ok, thanks.  That does make sense.


>> PS  Incidentally, it seems silly to have "is rw" but not "is ro".  I keep
>> writing "is ro".
> 
> Yes, we've also run into this problem a few times while working on Rakudo.

I'm finding it so verbose that I'm going with "is ro" in Method::Signatures.


-- 
I'm pale as formica, social skills stunted small. But I'm accurate
like a pica, I know the capital of nepal. I'm the nemesis of error,
dreadful diction fears my skills, more inquisitive than Jim Lehrer,
snottier than Beverly Hills.
-- I.L.O.P. Secret Rap  http://goats.com/archive/020830.html


Re: Subroutine parameter with trait and default.

2008-09-22 Thread TSa

HaloO,

Patrick R. Michaud wrote:
The STD.pm grammar [1] shows that the second is the correct form -- 
i.e., default values occur after traits.


IIRC, there used to be an 'is default(42)' trait that could
be placed arbitrarily.



PS  Incidentally, it seems silly to have "is rw" but not "is ro".  I keep
writing "is ro".


Yes, we've also run into this problem a few times while working on Rakudo.


Not to mention the fact that 'writeonly' and 'wo' are missing
completely even though they are required for proper contravariant
typing of output parameters. Parameters with 'rw' have to be
*invariant* unless we manage to split the meaning of the type slot
in 'rw' parameter declarations between input and output.


Regards, TSa.
--

"The unavoidable price of reliability is simplicity" -- C.A.R. Hoare
"Simplicity does not precede complexity, but follows it." -- A.J. Perlis
1 + 2 + 3 + 4 + ... = -1/12  -- Srinivasa Ramanujan


Re: Subroutine parameter with trait and default.

2008-09-22 Thread Patrick R. Michaud
On Sun, Sep 21, 2008 at 07:02:37PM -0700, Michael G Schwern wrote:
> I'm pondering what the proper syntax is for a subroutine parameter with both a
> trait and a default.  That is...
>   sub foo ($arg = 42)
> and
>   sub foo ($arg is readonly)
> together in one parameter.  Would that be
>   sub foo ($arg = 42 is readonly)
> or
>   sub foo ($arg is readonly = 42)
> 
> The first looks ambiguous, what if the trait is meant to apply to the default?
> The second looks downright wrong.


The STD.pm grammar [1] shows that the second is the correct form -- 
i.e., default values occur after traits.  (See "token parameter" 
on or about line 2630.)  I think part of the reason for this is that
traits appearing after the default value would be applied to the
default value instead of the parameter.  A few other examples from
the synopses seem to confirm this pattern:

  S03:2289:my Dog $fido is trained is vicious = 1
  S03:3828:constant Dog $foo is woof = 123;
  S06:1558:constant $pi is approximated = 3;
  S12:1393:has SomeType $.prop is rw = 1;

1.  http://svn.pugscode.org/pugs/src/perl6/STD.pm

> PS  Incidentally, it seems silly to have "is rw" but not "is ro".  I keep
> writing "is ro".

Yes, we've also run into this problem a few times while working on Rakudo.

Pm