use feature qw(state); # Perl 5.10+
use Type::Params qw(compile);
use Types::Standard qw(Num);
sub add {
state $check = compile( Num, Num );
my ($x, $y) = $check->(@_); # will croak if not passed two numbers
my $sum = $x + $y;
return $sum;
}
####
use feature qw(state); # Perl 5.10+
use Type::Params qw(compile);
use Types::Standard qw(Num);
sub add {
state $check = compile( Num, Num );
my ($x, $y) = $check->(@_); # will croak if not passed two numbers
my $sum = $x + $y;
Num->assert_return( $sum );
}
##
##
use feature qw(state); # Perl 5.10+
use Type::Params qw(compile);
use Types::Standard qw(ArrayRef);
use Carp;
use PerlX::Assert;
sub zip {
state $check = compile( ArrayRef, ArrayRef );
my ($x, $y) = $check->(@_);
# An additional check on input.
# Carp is a pretty appropriate way of dealing with this.
#
croak "Arrays must be same length" unless @$x == @$y;
my @z = map { [$x->[$i], $y->[$i]] } 0 .. $#$x;
# These assertions check the internal logic of our
# function, so using Carp makes less sense. It's not
# the caller's fault if they fail. Here an assertion
# feature makes sense. And because we believe
# our logic to be correct, it's hopefully okay to
# optimize this away in the production environment.
#
assert '@z has same length as @$x' { @z == @$x };
assert '@z has same length as @$y' { @z == @$y };
# \@z should be an arrayref of arrayrefs.
#
ArrayRef->of(ArrayRef)->assert_valid( \@z );
}