develooper Front page | perl.perl6.language | Postings from March 2005

Re: Parameter and trait questions - just how 'only' _is_ 'read-only'?

Thread Previous | Thread Next
From:
Luke Palmer
Date:
March 26, 2005 02:10
Subject:
Re: Parameter and trait questions - just how 'only' _is_ 'read-only'?
Message ID:
20050326101307.GA14855@navi.cx
Chip Salzenberg writes:
> I'm working on enhancing Perl6::Subs[*] to support more parameter
> traits than just C<is required>.  I have some questions about
> parameters and traits.  (These questions all apply to pure Perl 6,
> which I know I won't be able to translate completely, but I want to
> know which target I'm missing.)
> 
>  * Given a parameter C<Array @a>, it's obvious I'm not allowed to
>    C<push @a,1>, because I didn't say C<is rw>.  But am I allowed to
>    C<@a[0]++>?  How about C<@a[0][0]++>?  How deep is read-only-ness?

I believe that the default constant parameters is so we don't have to
construct lvalues out of our arguments when we call.  So that probably
means that it's very shallow.

On the language level, I've been thinking that it would be good to go
C's way and not allow any parameter modification whatsoever.  The
problem is that in the presence of methods, we can't tell whether we
have to lvaluize anymore, so we're back to the Perl 5 trap of lvaluizing
everything.  

So if you want things modified, you'd have to pass in a reference.
Arrays and hashes would not generally have this restriction, since we
pass references of those guys anyway.

>  * Similarly, how deep is the copy implied by C<is copy>?

I think it's exactly as deep as read-only-ness.

>  * Do traits attach syntactically to the variable name, or to the
>    declaration as a whole?
> 
>      variable:     @a is rw of Array
>                    Array @a is rw
> 
>      declaration:  @a of Array is rw
>                    Array @a is rw

Well, from this example it seems like `of` should be tighter than `is`.

>  * As far as I can tell, the choice of spelling an array parameter
>    C<Array @a> or C<Array $a> is entirely cosmetic: both @a and
>    $a are capable of holding an Array reference.  Is there actually
>    a difference, e.g. in how they handle an undefined value?

Hmmm... well I think all scalars are allowed to be undef.  Arrays
aren't.  So yeah, if you give @a undef, it probably gives you [] (or
croaks, but I don't think that's a good idea).  If you give $a undef, it
gives you undef.

> [*] Shameless Plug:  Perl6::Subs is a source filter that lets you use
>     much of the Perl 6 parameter syntax in your Perl 5 programs; and
>     it enforces many constraints for you.  You can even add your own
>     constraints with C<where BLOCK> subtyping.  Amaze your enemies!
>     Confound your friends!  Use Perl6::Subs today!

Cool.

Luke

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