Skip Menu |

This queue is for tickets about the Type-Tiny CPAN distribution.

Report information
The Basics
Id: 85732
Status: rejected
Priority: 0/
Queue: Type-Tiny

People
Owner: Nobody in particular
Requestors: DAMS [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: support for optional arguments
After reading your blog post [1] I tried to use it instead of Params::Validate (so not for attributes but function parameters parsing). However, Type::Tiny doesn't seem to be able to support optional parameters. I tried severral types, but it insist on the argument to exist, even if set to undef. Does Type::Tiny already supports optional parameters ? (positional especially), and if not, can it be added ? Thanks [1] http://blogs.perl.org/users/toby_inkster/2013/05/typetiny---not-just-for-attributes.html
There's an "Optional" parameterizable type defined in Types::Standard. There are several examples using it here: https://metacpan.org/module/Type::Params#COOKBOOK See in particular the subheadings: - Optional Parameters - Named Parameters - Mixed Positional and Named Parameters There are some limitations - required params must precede optional ones, which must precede slurpy params - but I believe Params::Validate has fairly similar limitations. If you need more complex unpacking of @_, like an optional hashref followed by a required string, then don't use Type::Params; unpack @_ yourself and check them using type constraint objects... use Types::Standard -types; sub optional_hashref_then_str { my $hashref; if (HashRef->check($_[0])) { $hashref = shift @_; } my $str = Str->(shift @_); ...; }
Thanks ! That did it.
Marking this as rejected. Feel free to re-open if you think there's anything in particular about optional parameter handling that needs fixing/improvement.