diff --git a/lib/Type/Params.pm b/lib/Type/Params.pm index 1007bdd8..4617a8ac 100644 --- a/lib/Type/Params.pm +++ b/lib/Type/Params.pm @@ -128,8 +128,7 @@ sub signature_for { if ( ref($function) eq 'ARRAY' ) { $opts{package} = $package; - signature_for( $_, %opts ) for @$function; - return; + return map { signature_for( $_, %opts ) } @$function; } $opts{_is_signature_for} = 1; @@ -167,7 +166,7 @@ sub signature_for { no warnings 'redefine'; *$fullname = set_subname( $fullname, $coderef ); - return; + return $sig; } sub compile { @@ -1519,6 +1518,23 @@ multiple functions with the same signature. positional => [ Num, Num ], ); +C does return a value. + + my $meta = signature_for add_nums => ( + positional => [ Num, Num ], + ); + + sub add_nums ( $x, $y ) { + return $x + $y; + } + +Or when used with multiple functions: + + my @metas = signature_for [ 'add_nums', 'subtract_nums' ] => (...); + +This is a blessed L object which provides some +introspection possibilities. + =head1 LEGACY API The following functions were the API prior to Type::Params v2. They are diff --git a/lib/Type/Params/Alternatives.pm b/lib/Type/Params/Alternatives.pm index 501e0e27..4c163ec4 100644 --- a/lib/Type/Params/Alternatives.pm +++ b/lib/Type/Params/Alternatives.pm @@ -1,5 +1,3 @@ -# INTERNAL MODULE: OO backend for Type::Params multisig-type signatures. - package Type::Params::Alternatives; use 5.008001; @@ -183,3 +181,86 @@ sub make_class_pp_code { 1; +__END__ + +=pod + +=encoding utf-8 + +=head1 NAME + +Type::Params::Alternatives - subclass of Type::Params::Signature for C signatures + +=head1 STATUS + +This module is not covered by the +L. + +=head1 DESCRIPTION + +This is mostly internal code, but can be used to provide basic introspection +for signatures. + +This module is a subclass of L, so inherits +attributes and methods from that. + +=head2 Constructor + +=over + +=item C<< new(%attributes) >> + +=back + +=head2 Attributes + +All attributes are read-only. + +=over + +=item C<< base_options >> B + +=item C<< alternatives >> B<< ArrayRef[HashRef|ArrayRef|CodeRef] >> + +=item C<< sig_class >> B + +=item C<< meta_alternatives >> B + +Automatically built from C; do not set this yourself. + +=item C<< parameters >> B + +Overridden from parent class to always return the empty arrayref. + +=item C<< message >> B + +Error message to be thrown when none of the alternatives match. +This is a bare attribute with no accessor method. + +=back + +=head1 BUGS + +Please report any bugs to +L. + +=head1 SEE ALSO + +L, L. + +=head1 AUTHOR + +Toby Inkster Etobyink@cpan.orgE. + +=head1 COPYRIGHT AND LICENCE + +This software is copyright (c) 2023-2024 by Toby Inkster. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=head1 DISCLAIMER OF WARRANTIES + +THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF +MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. diff --git a/lib/Type/Params/Parameter.pm b/lib/Type/Params/Parameter.pm index 16fc5b26..00058cc0 100644 --- a/lib/Type/Params/Parameter.pm +++ b/lib/Type/Params/Parameter.pm @@ -1,5 +1,3 @@ -# INTERNAL MODULE: a parameter within a Type::Params::Signature. - package Type::Params::Parameter; use 5.008001; @@ -365,3 +363,143 @@ sub _make_code { } 1; + +__END__ + +=pod + +=encoding utf-8 + +=head1 NAME + +Type::Params::Parameter - internal representation of a parameter in a function signature + +=head1 STATUS + +This module is not covered by the +L. + +=head1 DESCRIPTION + +This is mostly internal code, but can be used to provide basic introspection +for signatures. + +=head2 Constructor + +=over + +=item C<< new(%attributes) >> + +=back + +=head2 Attributes + +All attributes are read-only. + +=over + +=item C<< type >> B + +Type constraint for the parameter. + +=item C<< default >> B<< CodeRef|ScalarRef|Ref|Str|Undef >> + +A default for the parameter: either a coderef to generate a value, +a reference to a string of Perl code to generate the value, an +a reference to an empty array or empty hash, a literal string +to use as a default, or a literal undef to use as a default. + +=item C<< strictness >> B<< Bool|ScalarRef >> + +A boolean indicating whether to be stricter with type checks, +or a reference to a string of Perl code naming a Perl variable +or constant which controls strict behaviour. + +=item C<< clone >> B<< Bool >> + +The method for accessing this is called C for no +particular reason. + +=item C<< coerce >> B<< Bool >> + +Defaults to true if C has a coercion. + +=item C<< optional >> B<< Bool >> + +Defaults to true if there is a C or if C is a subtype of +B. + +=back + +=head3 Attributes related to named parameters + +=over + +=item C<< name >> B + +=item C<< alias >> B<< ArrayRef[Str] >> + +=item C<< getter >> B + +=item C<< predicate >> B<< Str >> + +=back + +=head2 Methods + +=head3 Predicates + +Predicate methods return true/false to indicate the presence or absence of +attributes. + +=over + +=item C<< has_type >> + +=item C<< has_default >> + +=item C<< has_strictness >> + +=item C<< has_name >> + +=item C<< has_alias >> + +=back + +=head3 Other methods + +=over + +=item C<< might_supply_new_value >> + +Indicates that the parameter can't simply be referenced within C<< @_ >> +because a default value might be used, the given value might be coerced, +or the given value might be cloned using L. + +=back + +=head1 BUGS + +Please report any bugs to +L. + +=head1 SEE ALSO + +L, L. + +=head1 AUTHOR + +Toby Inkster Etobyink@cpan.orgE. + +=head1 COPYRIGHT AND LICENCE + +This software is copyright (c) 2023-2024 by Toby Inkster. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=head1 DISCLAIMER OF WARRANTIES + +THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF +MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. diff --git a/lib/Type/Params/Signature.pm b/lib/Type/Params/Signature.pm index 634d01e4..b50a250e 100644 --- a/lib/Type/Params/Signature.pm +++ b/lib/Type/Params/Signature.pm @@ -1,5 +1,3 @@ -# INTERNAL MODULE: OO backend for Type::Params signatures. - package Type::Params::Signature; use 5.008001; @@ -1065,3 +1063,250 @@ sub return_wanted { } 1; + +__END__ + +=pod + +=encoding utf-8 + +=head1 NAME + +Type::Params::Signature - internal representation of a function signature + +=head1 STATUS + +This module is not covered by the +L. + +=head1 DESCRIPTION + +This is mostly internal code, but can be used to provide basic introspection +for signatures. + +=head2 Constructors + +=over + +=item C<< new(%attributes) >> + +=item C<< new_from_compile($style, %attributes) >> + +=item C<< new_from_v2api(\%attributes) >> + +=back + +=head2 Attributes + +All attributes are read-only. + +=over + +=item C<< package >> B + +The package we're providing a signature for. Will be used to look up any +stringy type names. + +=item C<< subname >> B + +The sub we're providing a signature for. + +=item C<< description >> B + +=item C<< method >> B<< ArrayRef[InstanceOf['Type::Params::Parameter']] >> + +=item C<< head >> B<< ArrayRef[InstanceOf['Type::Params::Parameter']] >> + +=item C<< tail >> B<< ArrayRef[InstanceOf['Type::Params::Parameter']] >> + +=item C<< parameters >> B<< ArrayRef[InstanceOf['Type::Params::Parameter']] >> + +=item C<< slurpy >> B<< InstanceOf['Type::Params::Parameter'] >> + +=item C<< on_die >> B + +=item C<< strictness >> B<< Bool|ScalarRef >> + +=item C<< goto_next >> B + +=item C<< can_shortcut >> B + +Indicates whether the signature has no potential to alter C<< @_ >> allowing +it to be returned without being copied if type checks pass. Generally speaking, +you should not provide this to the constructor and rely on +Type::Params::Signature to figure it out. + +=item C<< coderef >> B<< InstanceOf['Eval::TypeTiny::CodeAccumulator'] >> + +You probably don't want to provide this to the constructor. The whole point +of this module is to build it for you! + +=back + +=head3 Attributes related to named parameters + +=over + +=item C<< is_named >> B + +=item C<< bless >> B + +=item C<< class >> B + +=item C<< constructor >> B + +=item C<< class_attributes >> B + +HashRef suitable for passing to the C method of +L. A default will be generated based +on C + +=item C<< named_to_list >> B<< ArrayRef >> + +Can be coerced from a bool based on C. + +=item C<< oo_trace >> B + +Defaults to true. Indicates whether blessed C<< $arg >> hashrefs created by +the signature will include a C<< '~~caller' >> key. + +=back + +=head3 Bare attributes + +These attributes may be passed to the constructors and may do something, +but no methods are provided to access the values later. + +=over + +=item C<< positional >> or C<< pos >> B + +=item C<< named >> B + +=item C<< multiple >> or C<< multi >> B + +=item C<< returns >> B + +Shortcut for setting C and C simultaneously. + +=item C<< want_source >> B + +=item C<< want_details >> B + +=item C<< want_object >> B + +=item C<< rationalize_slurpies >> B + +=back + +=head2 Methods + +=head3 Predicates + +Predicate methods return true/false to indicate the presence or absence of +attributes. + +=over + +=item C<< has_description >> + +=item C<< has_head >> + +=item C<< has_tail >> + +=item C<< has_parameters >> + +=item C<< has_slurpy >> + +=item C<< has_on_die >> + +=item C<< has_strictness >> + +=item C<< has_returns_scalar >> + +=item C<< has_returns_list >> + +=back + +=head3 Class making methods + +These methods will be called automatically during object construction +and should not typically be called. They are public methods in case +it is desired to subclass Type::Params::Signature. + +=over + +=item C<< make_class_pp >> + +Builds the class specified in C by evaluating Perl code. + +=item C<< make_class_xs >> + +Builds the class specified in C using L. + +=item C<< make_class >> + +Calls either C or C. + +=item C<< make_class_pp_code >> + +Generates the code for C. + +=back + +=head3 Other methods + +=over + +=item C<< BUILD >> + +Called by the constructors. You should not call this. + +=item C<< return_wanted >> + +Normally returns the signature coderef, unless C, C, +or C were provided to the constructor, in which case it will +return the source code for the coderef, a hashref of details, or C<< $self >>. + +=back + +=head1 ENVIRONMENT + +=over + +=item C + +Affects the building of accessors for C<< $arg >> objects. If set to true, +will use L. If set to false, will use pure Perl. If this +environment variable does not exist, will use Class::XSAccessor. + +If Class::XSAccessor is not installed or is too old, pure Perl will always +be used as a fallback. + +=back + +=head1 BUGS + +Please report any bugs to +L. + +=head1 SEE ALSO + +L, L, L. + +=head1 AUTHOR + +Toby Inkster Etobyink@cpan.orgE. + +=head1 COPYRIGHT AND LICENCE + +This software is copyright (c) 2023-2024 by Toby Inkster. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=head1 DISCLAIMER OF WARRANTIES + +THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF +MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.