Skip to content

Commit 4eed22e

Browse files
committed
--> is not checked at compile time
1 parent a60d7ec commit 4eed22e

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

doc/Language/faq.pod

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,26 @@ Likewise C<:U> constrains to undefined values, that is, type objects.
197197
198198
To explicitly allow either type objects or instances, you can use C<:_>.
199199
200-
=head2 What is the --> thing in the signature?
200+
=head2 What is the C<< --> >> thing in the signature?
201201
202-
--> is a return constraint checked at compile time.
202+
C<< --> >> is a return constraint, either a type or a definite value.
203203
204-
Example:
204+
Example of a type constraint:
205205
206-
sub foo ( Int $a, Int $b --> Int ) {
207-
return $a + $b;
206+
sub divide-to-int( Int $a, Int $b --> Int ) {
207+
return ($a / $b).narrow;
208208
}
209209
210-
foo(2,3.1)
211-
# ===SORRY!=== ... Calling foo(Int, Rat) will never work with declared signature (Int $a, Int $b --> Int)
210+
divide-to-int(3, 2)
211+
# Type check failed for return value; expected Int but got Rat
212+
213+
Example of a definite return value:
214+
215+
sub discard-random-number( --> 42 ) { rand }
216+
say discard-random-number
217+
# 42
218+
219+
In this case the final value is thrown away because the return value is already specified.
212220
213221
=head2 How can I extract the values from a Junction?
214222

0 commit comments

Comments
 (0)