Function-Parameters icon indicating copy to clipboard operation
Function-Parameters copied to clipboard

Support array defaults

Open schwern opened this issue 10 years ago • 2 comments

$ perl -wle 'use Function::Parameters qw(:strict);  method foo(@bar = (1,2,3)) {}  main->foo()'
In method foo: array @bar can't have a default value at -e line 1.

I don't see why not. Is this an implementation or design issue?

schwern avatar Feb 22 '15 23:02 schwern

It's a language semantics issue. How do you distinguish an empty list from an absent list?

main->foo(()) or my $n = -1; my @values = 0 .. $n; main->foo(@values) shouldn't silently use some default values.

mauke avatar Feb 23 '15 07:02 mauke

It would be nice if we could distinguish between being passed an empty list and being passed no list at all, but we can't (can we?). That's one of the quirks of Perl's subroutines we have to live with. If you need to tell the difference between being passed no list and being passed an empty list, use an array ref. This is all how things already work. Perl programmers already know all this. Nobody writes Perl code that depends on the difference between an empty list and no list, you can't. I don't think there will be a great surprise when @foo = (1,2,3) is applied whether @foo is empty or never passed in at all.

It's exactly what they'll do anyway.

method foo(@bar) {
    @bar = (1,2,3) if !@bar;
    ...
}

What else are they going to do?

schwern avatar Feb 23 '15 21:02 schwern