From f6d54a7b9dd3a56a95b2148557f1fb67abd06a1b Mon Sep 17 00:00:00 2001 From: Moritz Lenz Date: Sat, 8 May 2010 19:08:32 +0200 Subject: [PATCH] [subs] where-blocks on parameters --- src/subs-n-sigs.pod | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/subs-n-sigs.pod b/src/subs-n-sigs.pod index 141c277..0764fea 100644 --- a/src/subs-n-sigs.pod +++ b/src/subs-n-sigs.pod @@ -607,6 +607,33 @@ the type constraint of the parameter it is bound to. =head2 Adding Constraints +X + +Sometimes a type name is not enough to sufficiently describe the parameters of +a subroutine. In this case an additional I can be added to the +parameter with a C block: + +=begin programlisting + + sub circle-radius-from-area(Numeric $area where { $area >= 0) { + ($area / pi).sqrt + } + say circle-radius-from-area(3); # OK + say circle-radius-from-area(-3); # Error + +=end programlisting + +Since the calculation can only be carried out for non-negative values of the +area, the parameter name is followed by a C block that returns C +for non-negative values. If such an additional constraint returns a false +value, the type check at the time of the routine call fails. + +=for author + +TODO: explain C smart matching constraints +once we talk about smart matching somewhere + +=end for =head1 Captures