@@ -242,50 +242,82 @@ on their signature write the signature after the argument name.
242
242
243
243
= head3 X « Constraining Return Types|-->;returns »
244
244
245
- The token C < -- > > followed by a type will force a type check on successful
246
- execution of a routine. The return type arrow has to be placed at the end of
247
- the parameter list. The keyword C < returns > following a signature declaration
248
- has the same function. C < Nil > and its subclasses do B < not > abide by return
249
- constraints. This allows L < Failure|/type/Failure > to be returned and passed on
250
- down the call chain.
251
-
252
- sub foo(--> Int) { my Int $i; $i };
253
- sub baz(--> Int:D) { 1 }
254
- sub bar() returns Int { 1 };
255
- sub does-not-work(--> Int) { "" }; # throws X::TypeCheck::Return
256
-
257
- There are two other ways to declare a return type:
258
-
259
- sub foo() of Int { 1 };
260
- my Int sub bar { 1 };
261
-
262
- C < of > is just the real name of the C < returns > keyword, and the
263
- prefix(C-like) form can be used too. Although all the ways are valid,
264
- the C < -- > > form is preferred for several reasons:
245
+ There are multiple ways to constrain return types on a sub or method. All versions below
246
+ are currently valid and will force a type check on successful execution of a routine.
247
+
248
+ L < C < Nil > |/type/Nil> and L < C < Failure > |/type/Failure> are always allowed as return types,
249
+ regardless of any type constraint. This allows L < Failure|/type/Failure > to be returned
250
+ and passed on down the call chain.
251
+
252
+ sub foo(--> Int) { Nil };
253
+ say foo.perl; # OUTPUT: «Nil»
254
+
255
+ Type captures and coercion types are not supported.
256
+
257
+ = item C < -- > >
258
+
259
+ This form is preferred for several reasons:
265
260
(1) it can handle constant values while the others can't;
266
- (2) for consistency, it is the only form accepted on this site; and
267
- (3) the C < returns > form is planned for future removal.
261
+ (2) for consistency, it is the only form accepted on this site;
268
262
269
- The following illustrates how to write a signature returning a constant value:
263
+ The return type arrow has to be placed at the end of the parameter list, with
264
+ or without a C < , > before it.
270
265
271
- = begin code :skip-test
272
- my 42 sub bad-answer {}; # This will fail.
273
- sub bad-answer2 returns 42 {}; # This too.
274
- sub answer(--> 42) { return; } # This doesn't.
275
- = end code
266
+ = begin code
267
+ sub greeting(Str $name --> Str) { say "Hello, $name" } # Valid
268
+ sub greeting(Str $name, --> Str) { say "Hello, $name" } # Valid
276
269
270
+ sub favorite-number(--> 42) { } # OUTPUT: 42
271
+ sub favorite-number(--> 42) { return } # OUTPUT: 42
272
+ = end code
273
+
277
274
If the type constraint is a constant expression, it is used as the return value
278
275
of the routine. Any return statement in that routine has to be argumentless.
279
276
280
- sub foo(--> 123) { return }
277
+ = begin code :skip-test
278
+ sub foo(Str $word --> 123) { say $word; return; }
279
+ my $value = foo("hello"); # OUTPUT: hello
280
+ say $value; # OUTPUT: 123
281
+
282
+ # The code below will not compile
283
+ sub foo(Str $word --> 123) { say $word; return $word; }
284
+ my $value = foo("hello");
285
+ say $value;
286
+ = end code
281
287
282
- L < C < Nil > |/type/Nil> and L < C < Failure > |/type/Failure> are always allowed as return types,
283
- regardless of any type constraint.
288
+ = item C < returns >
284
289
285
- sub foo(--> Int) { Nil };
286
- say foo.perl; # OUTPUT: «Nil»
290
+ The keyword C < returns > following a signature declaration
291
+ has the same function as C < -- > > with two caveats.
287
292
288
- Type captures and coercion types are not supported.
293
+ (1) This form is planned for future removal.
294
+ (2) This form does not work with constant values
295
+
296
+ = begin code :skip-test
297
+ sub greeting(Str $name) returns Str { say "Hello, $name" } # Valid
298
+
299
+ sub favorite-number returns 42 { } # This will fail.
300
+ = end code
301
+
302
+ = item C < of >
303
+
304
+ C < of > is just the real name of the C < returns > keyword.
305
+
306
+ = begin code :skip-test
307
+ sub foo() of Int { 42 }; # Valid
308
+
309
+ sub foo() of 42 { }; # This will fail.
310
+ = end code
311
+
312
+ = item prefix(C-like) form
313
+
314
+ This is similar to placing type constraints on variables like C < my Type $var = 20; > ,
315
+ except the $var is a definition for a routine.
316
+
317
+ = begin code :skip-test
318
+ my Int sub bar { 1 }; # Valid
319
+ my 42 sub bad-answer {}; # This will fail.
320
+ = end code
289
321
290
322
= head3 X < Coercion Type|coercion type (signature) >
291
323
0 commit comments