Skip to content

Commit 3cb4189

Browse files
committed
Attempt to explain ways to declare return type in signature
1 parent c5d8949 commit 3cb4189

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

doc/Type/Signature.pod6

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,19 @@ down the call chain.
251251
sub bar() returns Int { 1 };
252252
sub does-not-work(--> Int) { "" }; # throws X::TypeCheck::Return
253253
254+
There are two other ways to declare return type:
255+
256+
sub foo() of Int { 1 };
257+
my Int sub bar { 1 };
258+
259+
C<of> is just the real name of C<returns> keyword, and prefix(C-like) form can be used too. Although any of way is valid, C<-->> form differs from all others: it can handle constant values, while others don't.
260+
261+
=begin code :skip-test
262+
my 42 sub bad-answer {}; # This will fail.
263+
sub bad-answer2 returns 42 {}; # This too.
264+
sub answer(--> 42) { return; } # This doesn't.
265+
=end code
266+
254267
If the type constraint is a constant expression, it is used as the return value
255268
of the routine. Any return statement in that routine has to be argumentless.
256269

0 commit comments

Comments
 (0)