Function::Validation
sub validate-function
sub validate-function(
Sub $function,
Positional $arg_types,
Mu $return_type = Mu
) returns Bool
a function to validate the signature of other functions
class Mu $
a reference to the function you want to test
class Mu $
A list of the types. If unspecified use Mu
class Mu $
The expected return type of the function.
sub generate-func-validator
sub generate-func-validator(
Positional $arg_types,
Mu $return_type = Mu
) returns Sub
A function to generate a validator to validate the signature of functions
class Mu $
A list of the types. If unspecified use Mu
class Mu $
The expected return type of the function.
sub function-arg-types
sub function-arg-types(
Sub $function
) returns Seq
a function to extract the types of the arguments of a function
class Mu $
the function you want to know the argument types of
NAME
Function::Validation - Functions to help you validate the signature of functions
Think of it as a quasi-type system for functional programming.
SYNOPSIS
use Function::Validation;
#validate a specific function
my sub func(Int $x, Int $y, Bool :$translate, :$rotate) {};
validate-function(&func, (Int, Int, Bool, Any), Mu); # => True
# ^--inputs ^--return
# generate a reusable validator
my $validator = generate-func-validator((Int, Int, Bool, Any), Mu);
$validator(&func); # => True
my $lambda = sub (){"noparams"};
validate-function($lambda, [], Str); # => True
validate-function($lambda, [], Mu); # => True
validate-function($lambda, [], Any); # => False (Any isn't a catch-all like Mu)
my $lamda_validator = generate-func-validator([], Mu);
$lambda_validator($lambda); # => True
# extract the argument types from a function
my @types = function-arg-types(&func); #=> [(Int) (Int) (Bool) (Any)]
DESCRIPTION
Function::Validation validates a function you've been passed to ensure it takes the inputs you expect and provides the output you expect.
Useful in functional programming when you don't have any Classes and thus can't use a role
or class type to enforce valid input types.
AUTHOR
COPYRIGHT AND LICENSE
Copyright 2022
This library is free software; you can redistribute it and/or modify it under the MIT license