Skip to content

Commit

Permalink
type coercions work in returns
Browse files Browse the repository at this point in the history
RT#128964
  • Loading branch information
zoffixznet committed Sep 3, 2016
1 parent 1c1d01e commit dedfdf9
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion S06-signature/definite-return.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use v6;

use Test;

plan 19;
plan 20;

# L<S06/Signatures>

Expand Down Expand Up @@ -132,6 +132,78 @@ constant indiana-pi = 3;
ok $pointy() === Nil, 'pointy can have definite return type of Nil';
}

# RT #128964
subtest 'type coercions work in returns' => {
plan 8;

subtest 'sub (Int --> Str())' => {
plan 3;

my sub t (Int $x --> Str()) {$x}
isa-ok t(42), Str, 'returns correct type';
is t(42), "42", 'returns correct value';
is-deeply &t.returns, Str(Any), '.returns() gives correct value';
}

subtest 'sub (Num $x --> Int(Str))' => {
plan 3;

my sub t (Num $x --> Int(Str)) {"$x"}
isa-ok t(42e0), Int, 'returns correct type';
is t(42e0), 42, 'returns correct value';
is-deeply &t.returns, Int(Str), '.returns() gives correct value';
}

subtest 'sub (Int) returns Str()' => {
plan 3;

my sub t (Int $x) returns Str() {$x}
isa-ok t(42), Str, 'returns correct type';
is t(42), "42", 'returns correct value';
is-deeply &t.returns, Str(Any), '.returns() gives correct value';
}

subtest 'sub (Num) returns Int(Str)' => {
plan 3;

my sub t (Num $x) returns Int(Str) {"$x"}
isa-ok t(42e0), Int, 'returns correct type';
is t(42e0), 42, 'returns correct value';
is-deeply &t.returns, Int(Str), '.returns() gives correct value';
}

subtest 'block Int --> Str()' => {
plan 3;

my $block = -> Int $x --> Str() {$x};
isa-ok $block(42), Str, 'returns correct type';
is $block(42), "42", 'returns correct value';
is-deeply $block.returns, Str(Any), '.returns() gives correct value';
}

subtest 'block --> Str()' => {
plan 3;

my $block = -> --> Str() {42};
isa-ok $block(), Str, 'returns correct type';
is $block(), "42", 'returns correct value';
is-deeply $block.returns, Str(Any), '.returns() gives correct value';
}

subtest 'method (Int --> Str())' => {
plan 3;

my $o = class { method v (Int $x --> Str()) {$x} }.new;
isa-ok $o.v(42), Str, 'returns correct type';
is $o.v(42), "42", 'returns correct value';
is-deeply $o.^find_method('v').returns, Str(Any),
'.returns() gives correct value';
}

throws-like { sub (--> Str(Int)) { 42e0 }() }, X::TypeCheck::Return,
'returning incorrect type throws';
}

# returns vs -->
# indefinite vs immutable vs mutable
# what are some wacky immutable values that we could return? (*)
Expand Down

0 comments on commit dedfdf9

Please sign in to comment.