Skip to content

Commit

Permalink
refactor redeclaration of return type exceptions, enable "my Type sub…
Browse files Browse the repository at this point in the history
… 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
moritz committed Jul 31, 2012
1 parent 1b2db80 commit 6441778
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
16 changes: 16 additions & 0 deletions src/Perl6/Actions.pm
Expand Up @@ -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);

Expand Down
10 changes: 10 additions & 0 deletions src/Perl6/Metamodel/BOOTSTRAP.pm
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions src/core/traits.pm
Expand Up @@ -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!) {
Expand All @@ -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>(|$) { * }
Expand Down

0 comments on commit 6441778

Please sign in to comment.