Skip to content

Commit

Permalink
Improve smartmatching with junctions
Browse files Browse the repository at this point in the history
The last commit breaks smartmatching for junctions. While the compiler
could handle their threading when smartmatching, this would break any
existing code that has ACCEPTS candidates for junctions. Giving it a
default ACCEPTS candidate allows existing code to work again.

While not strictly necessary for this to work, this also moves handling
of junction smartmatching from Any to Mu. This allows `any(Mu, Any) ~~
Mu` to return `True` instead of `False`.
  • Loading branch information
Kaiepi committed Apr 10, 2020
1 parent 3b4794f commit 840d3e8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
11 changes: 4 additions & 7 deletions src/core.c/Any.pm6
Expand Up @@ -15,13 +15,10 @@ my role Numeric { ... }
my class Any { # declared in BOOTSTRAP
# my class Any is Mu

multi method ACCEPTS(Any:D: Mu:D \a) { self === a }
multi method ACCEPTS(Any:D: Mu:U $ --> False) { }

# use of Any on topic to force autothreading
# so that all(@foo) ~~ Type works as expected
multi method ACCEPTS(Any:U: Any \topic --> Bool:D) {
nqp::hllbool(nqp::istype(topic, self))
multi method ACCEPTS(Any:D: Mu:U --> False) { }
multi method ACCEPTS(Any:D: Mu:D \topic) {
# XXX: &[===] works with Any, not Mu!
self === topic
}

proto method EXISTS-KEY(|) is nodal {*}
Expand Down
8 changes: 1 addition & 7 deletions src/core.c/Junction.pm6
Expand Up @@ -176,12 +176,7 @@ my class Junction { # declared in BOOTSTRAP
)
}

multi method ACCEPTS(Junction:U: Mu:D \topic) {
nqp::hllbool(nqp::istype(topic, Junction));
}
multi method ACCEPTS(Junction:U: Any \topic) {
nqp::hllbool(nqp::istype(topic, Junction));
}
multi method ACCEPTS(Junction:U: Junction:D --> True) { }
multi method ACCEPTS(Junction:D: Mu \topic) {
nqp::hllbool(
nqp::stmts(
Expand Down Expand Up @@ -343,7 +338,6 @@ my class Junction { # declared in BOOTSTRAP
my int $elems = nqp::elems(positionals);
my int $i = -1;
while nqp::islt_i(++$i,$elems) {

# Junctional positional argument?
my Mu $arg := nqp::atpos(positionals, $i);
if nqp::istype($arg,Junction) {
Expand Down
9 changes: 9 additions & 0 deletions src/core.c/Mu.pm6
Expand Up @@ -21,6 +21,15 @@ my class Mu { # declared in BOOTSTRAP
multi method ACCEPTS(Mu:U: Mu \topic) {
nqp::hllbool(nqp::istype(topic, self))
}
# Typically, junctions shouldn't be typechecked literally. There are
# exceptions though, such as Junction in particular, so this probably
# shouldn't be handled by the compiler itself. Having a default ACCEPTS
# candidate to handle junctions allows them to get threaded as they should
# while preserving compatibility with existing code that has any ACCEPTS
# candidates for Mu or Junction.
multi method ACCEPTS(Mu:U \SELF: Junction:D \topic) is default {
topic.THREAD: { SELF.ACCEPTS: $_ }
}

method WHERE() {
nqp::p6box_i(nqp::where(self))
Expand Down

0 comments on commit 840d3e8

Please sign in to comment.