evalEmpire/method-signatures

Cannot make named parameters aliases

Closed this issue · 2 comments

As part of pull request #56, I asked @thoughtstream why \ (to make a parameter an alias for the passed-in variable) could not be combined with : (to indicate a named parameter). He replied:

The problem is that, without the check, MS current would unpack named and aliased parameters using code like this:

my %args = @_[0..$#_]; 
alias my $named_and_aliased = delete $args{named_and_aliased};

In other words, named args are copied into %args, then deleted (i.e. copied) out of it again. So you're aliasing a copy of a copy, not the original argument.

The generated code would have to be changed to something like:

my %args; 
alias $args{shift @_} = shift @_ while @_;
my $named_and_aliased = $args{named_and_aliased};
delete $args{named};

Feel free to do that if you like. It would certainly be a vast improvement.
(PS: Untested code above...don't rely on it!)

Damian

This should be fixed, and Damian's code added and tested, because the current restriction is unnecessary.

Once the current set of @thoughtstream mods has been integrated, I
intend to produce a new patch against that updated code. That
patch will provide for more efficient (i.e. with less copying and less
aliasing) extraction of named arguments, including the ability to
alias named args.

The code will not be exactly as described above: after further thought
I realise that there are more optimal ways to achieve the effect.

Damian

This is fixed in version 20130222.