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

Re: Optional binding

Thread Previous | Thread Next
Larry Wall
March 7, 2005 19:50
Re: Optional binding
Message ID:
On Mon, Mar 07, 2005 at 05:37:53PM -0800, David Storrs wrote:
: On Mon, Mar 07, 2005 at 04:58:29PM -0800, Larry Wall wrote:
: > 
: > In fact, we really haven't specified what happens when you say
: > 
: >     my Int @a is shape(3) := [1,2];
: >     my Int @b is shape(3) := [1,2,3,4];
: > 
: [...]
: > But I also have this nagging feeling that the user wouldn't have
: > specified shape(3) unless they actually meant it.  But I suspect
: > that in the typical case, they really do want a 3-element vector,
: > nothing more, nothing less.
: I agree that this is undoubtedly what they meant.  I wonder though, if
: there is then any way to explicitly leave off an element.  Can I do
: this:
: 	sub foo( Int @a is shape(3) ) { ... }
: 	foo(1, 2, undef);

That's illegal unless you either * the @a or [] the list.  By itself an
@a declaration is always looking to bind a single parameter.  Arguably,
1,2,undef could be interpreted as a single parameter, forcing the
scalar comma to auto-enreference a [1,2,undef] as it does when you say

    $foo = (1,2,undef);

but then we lose argument count checking on any sig ending in an
array, and the meaning of comma subtly changes without warning at
a place other than the expected scalar/variadic boundary.  So we
require splats to make that clear.  I'd also note that


would work in this case because parens in scalar context make all the args
into one arg.  (In list context they would flatten, as they do in Perl 5.)
However, it's a lot clearer to say


if that's what you mean.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About