Skip to content

Commit b1baf91

Browse files
committed
Merge pull request #303 from gfldex/master
doc Constraining Return Types
2 parents 276e344 + 95c4a87 commit b1baf91

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

doc/Type/Signature.pod

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,31 @@ say limit-lines "a \n b \n c", Int; # "a \n b \n c"
149149
For explicitly indicating the normal behaviour, C<:_> can be used, but this is
150150
unnecessary. C<:(Num:_ $)> is the same as C<:(Num $)>.
151151
152+
=head3 X«Constraining Return Types|-->;returns»
153+
154+
The token C<-->> followed by a type will force a type check on successful
155+
execution of a routine. The return type arrow has to be placed at the end of
156+
the parameter list. The keyword C<returns> following a signature declaration
157+
has the same function. C<Nil> is considered as being defined for the type check.
158+
159+
160+
sub foo(--> Int) { 1 };
161+
sub foo() returns Int { 1 };
162+
sub does-not-work(--> Int) { "" }; # throws X::TypeCheck::Return
163+
164+
If the type constrain is a constant expression, it is used as the return value
165+
of the routine. Any return statement in that routine has to be argumentless.
166+
167+
sub foo(--> 123) { return }
168+
169+
C<Nil> and C<Failure> are always allowed as return types, reguardless of any
170+
type contrain.
171+
172+
sub foo(--> Int) { Nil };
173+
say foo.perl; # Nil
174+
175+
Type captures and coercion types are not supported.
176+
152177
=head2 X<Slurpy (A.K.A. Variadic) Parameters|parameter,*@;parameter,*%>
153178
154179
An array or hash parameter can be marked as I<slurpy> by leading asterisk(s),

0 commit comments

Comments
 (0)