Skip to content

Commit

Permalink
Subtype parameters by their type constraints in Parameter.ACCEPTS
Browse files Browse the repository at this point in the history
Parameters can be of a type, but not accept all values of that type in a
number of different ways:

- Int $nat where * >= 0
- @tuple [$, $]
- |tuple ($, $)
- &f:($)

i.e. post constraints, subsignatures, and signature constraints.

Parameters do not smartmatch accordingly, but allowing them to do so is
a matter of removing some paranoid else blocks. While we're here, take
care of some nits with the other half of each.
  • Loading branch information
Kaiepi committed Oct 14, 2021
1 parent 088e0c4 commit e6a68a9
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions src/core.c/Parameter.pm6
Expand Up @@ -498,29 +498,22 @@ my class Parameter { # declared in BOOTSTRAP
}

# we have sub sig and not the same
my \osub_signature := nqp::getattr(o,Parameter,'$!sub_signature');
if $!sub_signature {
if nqp::isconcrete($!sub_signature) {
my \osub_signature := nqp::getattr(o,Parameter,'$!sub_signature');
return False
unless osub_signature
&& $!sub_signature.ACCEPTS(osub_signature);
unless nqp::isconcrete(osub_signature)
&& $!sub_signature.ACCEPTS(osub_signature);
}

# no sub sig, but other has one
elsif osub_signature {
return False;
}

my \osignature_constraint := nqp::getattr(o, Parameter, '$!signature_constraint');
if nqp::defined($!signature_constraint) {
return False unless nqp::defined(osignature_constraint)
&& $!signature_constraint.ACCEPTS(osignature_constraint);
}
else {
return False if nqp::defined(osignature_constraint);
if nqp::isconcrete($!signature_constraint) {
my \osignature_constraint := nqp::getattr(o, Parameter, '$!signature_constraint');
return False
unless nqp::isconcrete(osignature_constraint)
&& $!signature_constraint.ACCEPTS(osignature_constraint);
}

# we have a post constraint
if nqp::islist(@!post_constraints) {
if nqp::isconcrete(@!post_constraints) {

# callable means runtime check, so no match
return False
Expand All @@ -539,11 +532,6 @@ my class Parameter { # declared in BOOTSTRAP
nqp::atpos(opc,0));
}

# we don't, other *does* have a post constraint
elsif nqp::islist(nqp::getattr(o,Parameter,'@!post_constraints')) {
return False;
}

# it's a match!
True;
}
Expand Down

0 comments on commit e6a68a9

Please sign in to comment.