I've looked at how Perl 6 handles this in http://cloud.github.com/downloads/perl6/book/2012.05.23.a4.pdf but haven't found how they deal with the number of parameters not matching the signature.
Does anyone know how Perl 6 behaves?
perlito [1] doesn't warn or die if I pass more parameters than specified.
The specs [2] say that positional parameters are required per default and that "Passing the wrong number of required arguments to a normal subroutine is a fatal error.".
On Wed, Oct 24, 2012 at 11:58 PM, Alexander Hartmaier <alex.ha...@gmail.com> wrote:I've looked at how Perl 6 handles this in http://cloud.github.com/downloads/perl6/book/2012.05.23.a4.pdf but haven't found how they deal with the number of parameters not matching the signature.
Does anyone know how Perl 6 behaves?
perlito [1] doesn't warn or die if I pass more parameters than specified.
The specs [2] say that positional parameters are required per default and that "Passing the wrong number of required arguments to a normal subroutine is a fatal error.".
$ perl6
> sub f ($x, $y) { say "$x & $y" }
f
> f 1, 2;
1 & 2
> f 1, 2, 3;
Too many positional parameters passed; got 3 but expected 2
>
On Wed, Oct 24, 2012 at 8:43 PM, Eirik Berg Hanssen <Eirik-Ber...@allverden.no> wrote:
$ perl6
> sub f ($x, $y) { say "$x & $y" }
f
> f 1, 2;
1 & 2
> f 1, 2, 3;
Too many positional parameters passed; got 3 but expected 2
>
Can you post what f 1; would look like?
Another examples Yves brought up:
for my $sub (@subs) {
$sub->(@args);
}
Not even detectable in principle.
And die at compile time is part of number 1, if feasible. Pardon me if that wasnt clear.
> Personally I really hate the idea of it dieing.
>
> I have many time done something like:
>
> for my $sub (@subs) {
> $sub->(@args);
> }
>
> and relied on the fact that subs that take fewer args than are in
> @args will happily ignore the additional ones I have passed in.
It is actually the caller who needs to be able to decide whether a call
with too many arguments should die or not. Leaving the decision to the
callee would be nonsense.
We already have syntax to let the caller decide, in another case.
And it’s ugly, so people will want to avoid using it, and it will stand
out like a sore thumb to them when someone uses it. This is a feature.
Note that `$sub->(@args)` and `&$sub(@args)` are indistinguishable right
now. I propose that the parser remember this difference so that Yves
could write his example as the latter whereas the former would throw an
error if too many arguments were passed.I think this a fine solution. Thanks Aristotle.
* The Sidhekin <sidh...@allverden.no> [2012-10-27 18:30]:
> I think it's too subtle.Maybe. It’s at least plenty ugly.
… up to the point where you propose that it be a stricture that is
enabled without explicit request. No other stricture works that way and
I do not believe the exception is even necessary, so let’s please not
add one. After all, `use 5.012` or higher will enable all strictures. So
whichever perl version enables these signatures by default could also
enable the stricture in the same version feature bundle. Problem solved.
* Eirik Berg Hanssen <Eirik-Ber...@allverden.no> [2012-10-27 21:10]:
> On Sat, Oct 27, 2012 at 7:09 PM, Aristotle Pagaltzis <paga...@gmx.de>wrote:Actually now I am turned me off of the whole idea of implementing this
> > … up to the point where you propose that it be a stricture that is
> > enabled without explicit request. No other stricture works that way
> > and I do not believe the exception is even necessary, so let’s
> > please not add one. After all, `use 5.012` or higher will enable all
> > strictures. So whichever perl version enables these signatures by
> > default could also enable the stricture in the same version feature
> > bundle. Problem solved.
>
> Well, except that the caller need not be in the same scope as the
> declaration, and so need not be in scope of any C<< use 5.012 >>, and
> won't get that stricture ...
as a stricture. So old code is getting a stricture enabled on it that it
didn’t ask for explicitly, even when it hasn’t turned on any strictures?
Then what do you do with `no strict` in old code – does it disable the
new stricture? But the new stricture didn’t exist when that `no strict`
line was written. So does it leave it enabled? So then what does a bare
`use strict` do – does it enable it, or is it kept symmetric with the
`no strict` set?
There are no good answers to any of these questions, ergo `strict` is
the wrong place for this.
Are we generally agreed that strictness/laxness should be controllable by the caller, with strict as the default, and the lax behavior matching what I've already implemented?
I would agree as well, but here are some thoughts:On Sat, 2012-10-27 at 22:57 +0200, Aristotle Pagaltzis wrote:
> * Peter Martini <peterc...@gmail.com> [2012-10-27 22:40]:
> > Are we generally agreed that strictness/laxness should be controllable
> > by the caller, with strict as the default, and the lax behavior
> > matching what I've already implemented?
>
> I hope so but there is no way to know.
>
> I have been wishing more people would chime in on this subthread.
- This would be a lexical switch, correct? Meaning it won't disable
strictness inside the called sub?
- The current proposal of 'arity' sounds like it would apply to
additional and missing arguments. While I'd personally be very happy
about that, it seems to me people might want default-missing-to-undef
instead.
So, with that rules, perhaps a pragma is a wrong solution for bypassing arity checks.
for my $sub (@subs) {
$sub->(@args[ 0..signature($sub)->arity-1 ]);
}
for my $sub (@subs) {
$sub->(@args[ signature($sub)->positions ]); # for utility?
}
for my $sub (@subs) {
signature($sub)->laxcall($sub, @args); # for even more utility?
}