Skip to content

Commit

Permalink
Make a single named param match @ sig in MAIN
Browse files Browse the repository at this point in the history
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 87d219e
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/core/Main.pm6
Expand Up @@ -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;
Expand All @@ -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),
Expand Down

0 comments on commit 87d219e

Please sign in to comment.