Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactor redeclaration of return type exceptions, enable "my Type sub…

… f" return types

this refactors the check if a signature has a return type into
a has_returns method in Signature, which reduces the monkey-getattr accesses
into Signature
  • Loading branch information...
commit 6441778f61b8bb7682eb05140c1166021e63f286 1 parent 1b2db80
@moritz moritz authored
View
16 src/Perl6/Actions.pm
@@ -1761,6 +1761,22 @@ class Perl6::Actions is HLL::Actions {
$*W.attach_signature($code, $signature);
$*W.finish_code_object($code, $block, $*MULTINESS eq 'proto', :yada(is_yada($/)));
+ # attach return type
+ if $*OFTYPE {
+ my $sig := $code.signature;
+ if $sig.has_returns {
+ my $prev_returns := $sig.returns;
+ $*W.throw($*OFTYPE, 'X::Redeclaration',
+ what => 'return type for',
+ symbol => $code,
+ postfix => " (previous return type was "
+ ~ $prev_returns.HOW.name($prev_returns)
+ ~ ')',
+ );
+ }
+ $sig.set_returns($*OFTYPE.ast);
+ }
+
# Document it
Perl6::Pod::document($/, $code, $*DOC);
View
10 src/Perl6/Metamodel/BOOTSTRAP.pm
@@ -369,6 +369,16 @@ BEGIN {
nqp::bindattr(pir::perl6_decontainerize__PP($self),
Signature, '$!returns', pir::perl6_decontainerize__PP($type));
}));
+ Signature.HOW.add_method(Signature, 'has_returns', static(sub ($self) {
+ pir::perl6_booleanize__PI(
+ nqp::not_i(
+ nqp::isnull(
+ nqp::getattr(pir::perl6_decontainerize__PP($self),
+ Signature, '$!returns')
+ )
+ )
+ );
+ }));
# class Parameter {
# has str $!variable_name
View
16 src/core/traits.pm
@@ -163,11 +163,11 @@ multi trait_mod:<of>(Mu:U $target, Mu:U $type) {
$target.HOW.set_of($target, $type);
}
multi trait_mod:<of>(Routine:D $target, Mu:U $type) {
- my $returns := nqp::getattr(nqp::p6decont($target.signature), Signature, '$!returns');
+ my $sig := $target.signature;
X::Redeclaration.new(what => 'return type for', symbol => $target,
- postfix => " (previous return type was {$returns.^name})").throw
- if not nqp::p6bool(nqp::isnull($returns));
- $target.signature.set_returns($type)
+ postfix => " (previous return type was {$sig.returns.^name})").throw
+ if $sig.has_returns;
+ $sig.set_returns($type)
}
multi trait_mod:<is>(Routine:D $r, :$hidden_from_backtrace!) {
@@ -179,11 +179,11 @@ multi trait_mod:<is>(Routine:D $r, :$hidden_from_backtrace!) {
proto trait_mod:<returns>(|$) { * }
multi trait_mod:<returns>(Routine:D $target, Mu:U $type) {
- my $returns := nqp::getattr(nqp::p6decont($target.signature), Signature, '$!returns');
+ my $sig := $target.signature;
X::Redeclaration.new(what => 'return type for', symbol => $target,
- postfix => " (previous return type was {$returns.^name})").throw
- if not nqp::p6bool(nqp::isnull($returns));
- $target.signature.set_returns($type)
+ postfix => " (previous return type was {$sig.returns.^name})").throw
+ if $sig.has_returns;
+ $sig.set_returns($type)
}
proto trait_mod:<as>(|$) { * }
Please sign in to comment.
Something went wrong with that request. Please try again.