Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

RFC: new API for Type::Params

by tobyink (Canon)
on Aug 18, 2022 at 07:57 UTC ( [id://11146214]=perlquestion: print w/replies, xml ) Need Help??

tobyink has asked for the wisdom of the Perl Monks concerning the following question:

Firstly, I'm not planning on breaking compatibility with Type::Params. The new API would live under a different namespace, such as Type::Params2.

The API for Type::Params is currently:

use feature 'state'; use Type::Params qw( compile compile_named_oo ); use Types::Standard -types; sub function_with_positional_parameters { state $check = compile( ArrayRef, Int, Int ); my ( $list, $start, $end ) = $check->( @_ ); my @slice = @{$list}[ $start .. $end ]; return \@slice; } sub function_with_named_parameters { state $check = compile_named_oo( list => ArrayRef, start => Int, end + => Int ); my ( $arg ) = $check->( @_ ); my @slice = @{$arg->list}[ $arg->start .. $arg->end ]; return \@slice; }

Alternatively, there's:

use Type::Params qw( wrap_subs compile_named_oo ); use Types::Standard -types; wrap_subs function_with_positional_parameters => [ ArrayRef, Int, Int +]; sub function_with_positional_parameters { my ( $list, $start, $end ) = @_; my @slice = @{$list}[ $start .. $end ]; return \@slice; } wrap_subs function_with_named_parameters => compile_named_oo( list => ArrayRef, start => Int, end => Int ); sub function_with_named_parameters { my ( $arg ) = @_; my @slice = @{$arg->list}[ $arg->start .. $arg->end ]; return \@slice; }

My suggested API is:

use feature 'state'; use Type::Params2; use Types::Standard -types; sub function_with_positional_parameters { state $check = signature( pos => [ ArrayRef, Int, Int ], ); my ( $list, $start, $end ) = $check->( @_ ); my @slice = @{$list}[ $start .. $end ]; return \@slice; } sub function_with_named_parameters { state $check = signature( named => [ list => ArrayRef, start => Int, end => Int ], ); my ( $arg ) = $check->( @_ ); my @slice = @{$arg->list}[ $arg->start .. $arg->end ]; return \@slice; }

It would also support the inside-out technique:

use Type::Params2; use Types::Standard -types; signature_for function_with_positional_parameters => ( pos => [ ArrayRef, Int, Int ], ); sub function_with_positional_parameters { my ( $list, $start, $end ) = @_; my @slice = @{$list}[ $start .. $end ]; return \@slice; } signature_for function_with_named_parameters => ( named => [ list => ArrayRef, start => Int, end => Int ], ); sub function_with_named_parameters { my ( $arg ) = @_; my @slice = @{$arg->list}[ $arg->start .. $arg->end ]; return \@slice; }

There would be a shortcut for methods:

signature_for method_with_named_parameters => ( method => 1, named => [ list => ArrayRef, start => Int, end => Int ], ); sub method_with_named_parameters { my ( $self, $arg ) = @_; my @slice = @{$arg->list}[ $arg->start .. $arg->end ]; return \@slice; }

Comments? Do people think this would be an improvement?

Replies are listed 'Best First'.
Re: RFC: new API for Type::Params
by Arunbear (Prior) on Aug 19, 2022 at 10:01 UTC
    Yes, I think it would be an improvement in that the new names more accurately describe the purpose of those functions.

    Having said that, I really prefer the approach of Params::Validate (it may not be the fastest choice, but having minimal boilerplate makes it more appealing).

    Update ... Perhaps you can add a minimal boilerplate option for folks who can live with the speed hit?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11146214]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 16:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found