summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/Type/Tiny/Manual/Optimization.pod25
1 files changed, 23 insertions, 2 deletions
diff --git a/lib/Type/Tiny/Manual/Optimization.pod b/lib/Type/Tiny/Manual/Optimization.pod
index 13c43615..13253ad4 100644
--- a/lib/Type/Tiny/Manual/Optimization.pod
+++ b/lib/Type/Tiny/Manual/Optimization.pod
@@ -223,15 +223,19 @@ it can lead to inconsistent and unpredictable behaviour.
=head3 Type::Params
+Very efficient way which avoids compiling the signature at all if
+C<STRICT> is false:
+
use Types::Standard qw( Num Object );
use Type::Params qw( compile );
use Devel::StrictMode qw( STRICT );
sub add_number {
state $check;
- $check = compile(Object, Num) if STRICT;
+ $check = compile( Object, Num ) if STRICT;
+
+ my ( $self, $num ) = STRICT ? &$check : @_;
- my ($self, $num) = STRICT ? $check->(@_) : @_;
push @{ $self->numbers }, $num;
return $self;
}
@@ -239,6 +243,23 @@ it can lead to inconsistent and unpredictable behaviour.
Again, you need to be careful to ensure consistent behaviour if you're
using coercions, defaults, slurpies, etc.
+Less efficient way, but more declarative and smart enough to just disable
+checks which are safe(ish) to disable, while coercions, defaults, and
+slurpies will continue to work:
+
+ use Types::Standard qw( Num Object );
+ use Type::Params qw( compile );
+ use Devel::StrictMode qw( STRICT );
+
+ sub add_number {
+ state $check = compile( { strictness => STRICT }, Object, Num );
+
+ my ( $self, $num ) = &$check;
+
+ push @{ $self->numbers }, $num;
+ return $self;
+ }
+
=head3 Ad-Hoc Type Checks
...;