Skip to content

Instantly share code, notes, and snippets.

@dakkar
Last active February 22, 2024 16:18
Show Gist options
  • Save dakkar/97848d044d88890e40095dda74b86026 to your computer and use it in GitHub Desktop.
Save dakkar/97848d044d88890e40095dda74b86026 to your computer and use it in GitHub Desktop.
role to execute a matching sub in raku
use v6.d;
use experimental :macros;
role mmm {
method ACCEPTS(Code:D: |c) {
dd self;dd c; # debugging!
return False unless c ~~ self.signature;
self.(|c);
return True;
}
}
macro matches($xxx) {
quasi { when ({{{$xxx}}} but mmm) { }; };
}
class Point {
has $.x;
has $.y;
}
given Point.new(:5x,:3y) {
matches -> (:$a) {
say "something a=$a";
};
matches -> Point:D (:$x, :$y) {
say "point $x $y";
};
matches -> (:$x,:$y) {
say "other $x $y";
};
};
use v6.d;
role mmm {
method ACCEPTS(Code:D: |c) {
return False unless c ~~ self.signature;
self.(|c);
return True;
}
}
class Point {
has $.x;
has $.y;
}
given Point.new(:5x,:3y) {
when -> (:$a) {
say "something a=$a";
} but mmm {};
when -> Point:D (:$x, :$y) {
say "point $x $y";
} but mmm {};
when -> (:$x,:$y) {
say "other $x $y";
} but mmm {};
};

the first program works

the second program… really doesn't:

-> $ (:$a) { #`(Block+{mmm}|2199162782080) ... }
\(AST.new)
Use of uninitialized value element of type Mu in string context.
Methods .^name, .raku, .gist, or .say can be used to stringify it to something meaningful.
  in block  at /tmp/d.raku line 24
something a=
-> Point:D $ (:$x, :$y) { #`(Block+{mmm}|2199162862416) ... }
\(AST.new)
-> $ (:$x, :$y) { #`(Block+{mmm}|2199162862488) ... }
\(AST.new)
Use of uninitialized value element of type Mu in string context.
Methods .^name, .raku, .gist, or .say can be used to stringify it to something meaningful.
  in block  at /tmp/d.raku line 30
Use of uninitialized value element of type Mu in string context.
Methods .^name, .raku, .gist, or .say can be used to stringify it to something meaningful.
  in block  at /tmp/d.raku line 30
other
When invoking 8 '', provided outer frame 0x20003c2ed00 (32 '') does not match expected static frame 0x20003c2e600 (26 '')
  in any  at /tmp/d.raku line 14
  in block <unit> at /tmp/d.raku line 29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment