mauke / Function-Parameters

Function::Parameters - define functions and methods with parameter lists ("subroutine signatures")
https://metacpan.org/pod/Function::Parameters
18 stars 19 forks source link

Support Multiple Shifted Args (for method modifiers) #23

Closed danschmidt5189 closed 7 years ago

danschmidt5189 commented 9 years ago

It would be nice if F::P offered a shortcut for working with Moose's around modifier, e.g.:

package My::FP;
use Function::Parameters;
sub import {
    Function::Parameters->import({
        ...
        decorator => { shift => [qw($orig $self)], ... },
    });
}

package Some::Role;
use Moose::Role;
use My::FP;
around some_sub => decorator (@args) {
    # $orig *and* $self already shifted off
};

(Right now you're only able to shift off a single argument.)

dstroma commented 7 years ago

I would like to see this as well.

A simpler example...

use Function::Parameters { 
  pair_function => { defaults => 'function_strict', shift => [qw($x $y)] }
};

pair_function swap {
  return ($y, $x);
}

pair_function multiply {
  return $x * $y;
}

pair_function marry {
  say "I now pronounce $x and $y husband and wife!";
}
tobyink commented 7 years ago

This can be done in Kavorka. Probably needs to be made easier though.

use v5.14;
use warnings;
use Kavorka ();

# This would typically be in an external file.
BEGIN {
  package My::Function::Pair;
  use Moo;
  with 'Kavorka::Sub';
  sub default_invocant {
    require Kavorka::Parameter;
    return (
      'Kavorka::Parameter'->new(name => '$x', traits => { invocant => 1 }),
      'Kavorka::Parameter'->new(name => '$y', traits => { invocant => 1 }),
    );
  }
};

use Kavorka '-default', pair_function => { implementation => 'My::Function::Pair' };

# This function uses $x and $y defined above.
pair_function marry {
  say "I now pronounce $x and $y husband and wife!";
  say for @_;
}

marry("Bob", "Alice", "Those two were shifted off.");

# This one overrides $x and $y using its own variable names.
pair_function marry2 ($husband, $wife: ...) {
  say "I now pronounce $husband and $wife husband and wife!";
  say for @_;
}

marry2("Dave", "Carol", "Those two were shifted off.");
mauke commented 7 years ago

This is now possible with version 2 (already released to CPAN).