-
-
Save moritz/2379772 to your computer and use it in GitHub Desktop.
Auto-generated dependent comparison operators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use AutoGen; | |
class A { | |
has $.x; | |
multi infix:<cmp>(A $a, A $b) is autogen { | |
$a.x cmp $b.x; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module AutoGen { | |
my %names = | |
cmp => <before after>, | |
leg => <gt lt ge le eq>, | |
'<=>' => ('>', '<', '>=', '<=', '==') | |
; | |
multi trait_mod:<is>(&routine, :$autogen!) is export { | |
my @ops = | |
sub ($a, $b) { routine($a, $b) == Increase }, | |
sub ($a, $b) { routine($a, $b) == Decrease }, | |
sub ($a, $b) { routine($a, $b) != Decrease }, | |
sub ($a, $b) { routine($a, $b) != Increase }, | |
sub ($a, $b) { routine($a, $b) == Same }, | |
; | |
my $shortname = &routine.name.substr(7, *-1); | |
for %names{$shortname}.list Z @ops -> $n, $o { | |
my role named { has $.name }; | |
$o does named("infix:<$n>"); | |
for &routine.signature.params.kv -> $i, $p { | |
nqp::bindattr(nqp::p6decont($o.signature.params[$i]), | |
Parameter, '$!nominal_type', $p.type); | |
} | |
trait_mod:<is>(:export, $o); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./perl6 -e 'use A; say A.new(x => 5) before A.new(x => 7)' | |
True | |
$ ./perl6 -e 'use A; say "a" before "b"' | |
===SORRY!=== | |
CHECK FAILED: | |
Calling 'infix:<before>' will never work with argument types (str, str) (line 1) | |
Expected: :(A $a, A $b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment