Skip to content
Permalink
Browse files

Make a single named param match @ sig in MAIN

Fixes R#2797.  This does **not** rewrite the signature binder.  Instead,
if the generated capture can not find any candidates, the capture changes
any named scalar values into 1 element arrays, and tries to find candidates
again.  If that is succesful, the modified signature is dispatched.

This is really a hack, but will in most cases do the right.
  • Loading branch information...
lizmat committed Mar 26, 2019
1 parent 89b4467 commit 87d219ee68f53855957f6f151fa9f88106ce7dfa
Showing with 25 additions and 6 deletions.
  1. +25 −6 src/core/Main.pm6
@@ -266,6 +266,22 @@ my sub RUN-MAIN(&main, $mainline, :$in-as-argsfiles) {
False
}

sub find-candidates($capture) {
&main
# Get a list of candidates that match according to the dispatcher
.cando($capture)
# Sort out all that would fail due to binding
.grep: { !has-unexpected-named-arguments(.signature, $capture.hash) }
}

# turn scalar values of nameds into 1 element arrays, return new capture
sub scalars-into-arrays($capture) {
my %hash = $capture.hash.map: {
nqp::istype(.value,Positional) ?? $_ !! Pair.new(.key,[.value])
}
Capture.new( :list($capture.list), :%hash)
}

# set up other new style dynamic variables
my &*ARGS-TO-CAPTURE := &default-args-to-capture;
my &*GENERATE-USAGE := &default-generate-usage;
@@ -274,14 +290,17 @@ my sub RUN-MAIN(&main, $mainline, :$in-as-argsfiles) {
my $capture := args-to-capture(&main, @*ARGS);

# Get a list of candidates that match according to the dispatcher
my @matching_candidates = &main.cando($capture);

# Sort out all that would fail due to binding
@matching_candidates .=
grep: { !has-unexpected-named-arguments(.signature, $capture.hash) };
my @candidates = find-candidates($capture);
if !@candidates {
my $alternate = scalars-into-arrays($capture);
if find-candidates($alternate) -> @alternates {
$capture := $alternate;
@candidates = @alternates;
}
}

# If there are still some candidates left, try to dispatch to MAIN
if @matching_candidates {
if @candidates {
if $in-as-argsfiles {
my $*ARGFILES := IO::ArgFiles.new: (my $in := $*IN),
:nl-in($in.nl-in), :chomp($in.chomp), :encoding($in.encoding),

0 comments on commit 87d219e

Please sign in to comment.
You can’t perform that action at this time.