Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make .Capture of certain core types throw
To avoid supersticious, performance-reducing parens;
e.g. -> ($x) {} where -> $x {} was meant.

Discussion: https://irclog.perlgeek.de/perl6/2017-03-07#i_14221839
  • Loading branch information
zoffixznet committed Oct 9, 2017
1 parent 4ba12ff commit bad9fef
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/core/Callable.pm
@@ -1,6 +1,9 @@
my class X::Cannot::Capture { ... }

my role Callable[::T = Mu] {
method of() { T }
method returns() { T }
method Capture() { die X::Cannot::Capture.new: :what(self) }
}

# vim: ft=perl6 expandtab sw=4
5 changes: 5 additions & 0 deletions src/core/Failure.pm
Expand Up @@ -71,6 +71,11 @@ my class Failure is Nil {
)
}

method Capture() {
self.DEFINITE.not || $!handled
?? X::Cannot::Capture.new(what => self).throw
!! self!throw
}
method Int(Failure:D:) { $!handled ?? Int !! self!throw(); }
method Num(Failure:D:) { $!handled ?? NaN !! self!throw(); }
method Numeric(Failure:D:) { $!handled ?? NaN !! self!throw(); }
Expand Down
3 changes: 3 additions & 0 deletions src/core/Int.pm
@@ -1,4 +1,5 @@
my class Rat { ... }
my class X::Cannot::Capture { ... }
my class X::Numeric::DivideByZero { ... }
my class X::NYI::BigInt { ... }

Expand Down Expand Up @@ -38,6 +39,8 @@ my class Int does Real { # declared in BOOTSTRAP
nqp::p6bool(nqp::bool_I(self));
}

method Capture() { die X::Cannot::Capture.new: :what(self) }

method Int() { self }

multi method Str(Int:D:) {
Expand Down
2 changes: 2 additions & 0 deletions src/core/Num.pm
@@ -1,3 +1,4 @@
my class X::Cannot::Capture { ... }
my class X::Numeric::DivideByZero { ... }
my class X::Numeric::CannotConvert { ... }
my role Rational { ... }
Expand All @@ -19,6 +20,7 @@ my class Num does Real { # declared in BOOTSTRAP
ObjAt
)
}
method Capture() { die X::Cannot::Capture.new: :what(self) }
method Num() { self }
method Bridge(Num:D:) { self }
method Range(Num:U:) { Range.new(-Inf,Inf) }
Expand Down
4 changes: 4 additions & 0 deletions src/core/Signature.pm
@@ -1,3 +1,5 @@
my class X::Cannot::Capture { ... }

my class Signature { # declared in BOOTSTRAP
# class Signature is Any
# has @!params; # VM's array of parameters
Expand Down Expand Up @@ -79,6 +81,8 @@ my class Signature { # declared in BOOTSTRAP
True;
}

method Capture() { die X::Cannot::Capture.new: :what(self) }

method arity() {
$!arity
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/Str.pm
@@ -1,5 +1,6 @@
my class Range { ... }
my class Match { ... }
my class X::Cannot::Capture { ... }
my class X::Str::InvalidCharName { ... }
my class X::Str::Numeric { ... }
my class X::Str::Match::x { ... }
Expand Down Expand Up @@ -39,6 +40,7 @@ my class Str does Stringy { # declared in BOOTSTRAP
multi method Bool(Str:D:) {
nqp::p6bool(nqp::chars($!value));
}
method Capture() { die X::Cannot::Capture.new: :what(self) }

multi method Str(Str:D:) { self }
multi method Stringy(Str:D:) { self }
Expand Down
2 changes: 2 additions & 0 deletions src/core/Version.pm
Expand Up @@ -103,6 +103,8 @@ class Version {
True;
}

method Capture() { die X::Cannot::Capture.new: :what(self) }

multi method WHICH(Version:D:) {
nqp::box_s(
nqp::concat(
Expand Down
5 changes: 4 additions & 1 deletion src/core/Whatever.pm
@@ -1,15 +1,18 @@
class X::Cannot::New { ... }
my class X::Cannot::Capture { ... }
my class X::Cannot::New { ... }

my class Whatever {
multi method ACCEPTS(Whatever:D: $ --> True) { }
multi method perl(Whatever:D: --> '*') { }
multi method Str(Whatever:D: --> '*') { }
method Capture() { die X::Cannot::Capture.new: :what(self) }
}

my class HyperWhatever {
multi method new(HyperWhatever:) { X::Cannot::New.new(class => self).throw }
multi method ACCEPTS(HyperWhatever:D: $ --> True) { }
multi method perl(HyperWhatever:D:) { '**' }
method Capture() { die X::Cannot::Capture.new: :what(self) }
}

sub HYPERWHATEVER (&c) { sub (*@_) { map &c, @_ } }
Expand Down

0 comments on commit bad9fef

Please sign in to comment.