On Thu, 2002-04-11 at 09:59, Ariel Scolnicov wrote:
> [Apologies to Aaron Sherman, who gets this twice due to my
> dunderheadedness]

No problem. I usually reply to the person and CC the list because some
folks have filters that will make discussions easier if I'm replying to
them vs. sending just to the list. Everyone else can just shut up and
use procmail to filter common message IDs ;-)

> Aaron Sherman <[EMAIL PROTECTED]> writes:
> 
> [...]
> 
> > Also, another thought:
> > 
> >     sub foo($a = 1, $b, $c) { ... }
> > 
> > In C++ at least, I think this is an error. However, it seems to me that
> > in Perl it could be interpreted to be equivalent to:
> > 
> >     sub foo($a = 1, $b = undef, $c = undef) { ... }
> > 
> > Which, again, allows us to clean up the common case.
> 
> Not really.  The problem (and the reason C++ doesn't allow this) is
> that
> 
>    sub foo($a=1, $b, $c=3) { ... }
> 
> is ambiguous: While foo(2) sets $a=1, $b=2, $c=3, it's impossible to
> say what foo(4,5) should do.

Nope. Someone else has already pointed out what they expect the behavior
to be in this case (and I concur), but let me state the rules explicitly
(as I would imagine them):

1. The first default marks the beginning of optional parameters.
2. An optional parameter with no default will automatically default to
undef.

That's it. Simple as pie, and always non-ambiguous.

> One could also imagine a rule saying all optional matches occur
> right-to-left, no matter where they are; this would be very confusing,
> though.  It's also a bad idea, software-engineering-wise.  Say I start
> off with
> 
>    sub bar($a, $b=2, $c, $d=4) { ... }
> 
> So bar(11,12,13) matches 11,12,13 -> $a,$b,$c, and bar(1,3) matches
> 1,3 -> $a,$c.  All seems well, until I decide that since anyway $c is
> usually 3, I should change the signature and make $c optional too:

Yikes! I would never do that. Let me re-state my example:

        sub foo($a = 1, $b, $c) { ... }

would be the same as

        sub foo($a = 1, $b = undef, $c = undef) { ... }

So your example would silently become:

        sub bar($a, $b = 2, $c = undef, $d = 4) { ... }

This is much easier to understand. Here are some permutations:

        bar(1) => bar(1,2,undef,4);
        bar(1,3) => bar(1,3,undef,4);
        bar(1,3,8) => bar(1,3,8,4);
        bar(1,undef,3) => bar(1,undef,3,4);

The only thing that gets a little tricky is named pairs:

        bar(d=>1) => error: a not defined
        bar(a=>1, d=>8) => bar(1,2,undef,8);

Makes sense, no?


Reply via email to