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

Passing the same named parameter multiple times should be an error.

Open schwern opened this issue 10 years ago • 2 comments

As with hash initialization, you can specify the same key
multiple times and the last occurrence wins:

    rectangle(height => 1, width => 2, height => 2, height => 5);
    # same as: rectangle(width => 2, height => 5);

I don't see why this should be a documented feature. IMO it's going to be a mistake far more often than it's intended. The only time I can think this is a good idea is to override the defaults. Method::Signatures explicitly disallows it.

The same feature can be implemented by putting the options into a hash first. This has the advantage of being explicit. It's going to be pretty rare, so the extra code is ok.

my %defaults = (height => 1, width => 2);
my %args = (%defaults, %your_args);
rectangle(%args);

The Perl idea that parameters are just a list (or list being flattened into a hash) is a weird language quirk. Function parameters should not hold over those quirks.

schwern avatar Feb 23 '15 01:02 schwern

The first version of this feature actually did that. I scrapped it after performance testing: No matter what I did, I couldn't get it to run as fast as shoving everything into a hash first.

mauke avatar Feb 23 '15 07:02 mauke

What kind of performance difference are we talking?

schwern avatar Feb 23 '15 21:02 schwern