7

Is it possible to assign a signature to a variable and then reuse it in different functions/methods? I've found my $sig = :($a, $b); but I don't know how I could use the variable as a signature in a function.

sid_com
  • 24,137
  • 26
  • 96
  • 187

1 Answers1

7

One way:

my $sig = :( $a, $b );

sub foo ( &function where { .signature ~~ $sig } ) {}

sub bar    ( $p, $q ) {}
sub qux    ( $waldo ) {}

foo &bar;

say "OK at line 10"; # OK at line 10

foo &qux;            # Constraint type check failed ... line 12".
raiph
  • 31,607
  • 3
  • 62
  • 111