Just another Perl shrine | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Eagle-eyed watchers of CPAN may have noticed that I've recently been releasing Type::Tiny development releases with version numbers 1.999_XYZ. Type::Tiny v2 is intended to be compatible with Type::Tiny v1. If you've used Type::Tiny v1, you shouldn't need to change any code, but Type::Tiny v2 has a few new features which may make your code simpler, more maintainable, and more readable if you adopt them. Type::Params v2 APIType::Params can be used to provide typed subroutine signatures:
However, things like named paramaters, catering for $self in methods, etc felt like afterthoughts. Here is how you'd write the same signature in version 1 as a method call using named parameters:
While the old API is still supported, Type::Params v2 has two new functions, signature and signature_for, which I feel provide a more powerful and more consistent interface. signature works much the same as compile, but takes a top-level hash of options, allowing it to cater for both positional and named parameters. Here is an example for positional parameters:
Here is an example for named parameters:
And signature_for allows you to turn that definition inside-out.
Handy import shortcutsA handy way to define an Enum type in Type::Tiny 2 is:
You can use this in a class like:
Yes, Enum type constraints now provide constants like SIZE_XL above. Type::Tiny::Class provides a similar shortcut:
Type::Tiny::Role and Type::Tiny::Duck also provide shortcuts. Types::CommonHaving checked out a lot of modules which use Type::Tiny, I've noticed that the most common modules people import from are Types::Standard, Type::Params, Types::Common::Numeric, and Types::Common::String. Types::Common is a new module that combines all of the above. For quick scripts and one-liners, something like this may save a bit of typing:
Though like always, you can list imports explicitly:
A type divided against itself shall standYou can now divide a type constraint by another:
What does this mean? Under normal circumstances, Num/Any evaluates to just Any. Num is basically just documentation, so you're documenting that lucky_numbers is intended to be an arrayref of numbers, but as a speed boost, the attribute will just check that it's an arrayref of anything. When the EXTENDED_TESTING environment variable is switched on though, Num/Any will evaluate to Num, so stricter type checks will kick in. Type defaultsInstead of this:
You can now write this:
This is more typing, so why do this? Well, for ArrayRef it might be more typing, but in this case:
It might be neater to include the default in the definition of your ColourScheme type. The new DelimitedStr typeTypes::Common::String now has a DelimitedStr type. This allows DelimitedStr[ "|", Int ] to accept strings like "12|34|-99|0|1". InternalsThere have been numerous internal refactorings in Type::Tiny v2, so if you're using Type::Tiny and its related modules in more unorthodox ways, it may be worth explicitly testing your code still runs on the new version. However, I have taken care to avoid breaking any documented APIs. The vast majority of the Type:Tiny v1 test suite still passes with Type::Tiny v2, with test cases that inspect the exact text of error messages being the only real change. In reply to Type::Tiny v2 is Coming by tobyink
|
|