Skip Menu |

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

Report information
The Basics
Id: 120226
Status: resolved
Priority: 0/
Queue: Type-Tiny

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

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



Subject: Provide method to inline an attribute type check
I would like a method to inline an attribute type check. Currently, what I'm doing is this: https://github.com/haarg/MooX-TypeTiny/blob/master/lib/Method/Generate/Accessor/Role/TypeTiny.pm It has to do a number of things that feel like they should be part of Type::Tiny. ->inline_assert is close to what I need, but has a few problems. First is that it won't work for non-inlinable types. If it could be provided with an inlinable string to use for the type, it could do similar to MooX::TypeTiny and return either $check->inline_check($value) or "${var}->check($value)". This seems like a useful option for any type check, not just for attributes. Second is that it provides no way to pass in attribute information, such as attribute_name, attribute_step, or varname. The assertion class will pick them up from $Method::Generate::Accessor::CurrentAttribute, but I don't want to have to set that up before knowing if the type check passed. Third is that Error::TypeTiny::Assertion->new will override any passed in attributes with values from $Method::Generate::Accessor::CurrentAttribute if it is set. In MooX::TypeTiny I have to localize $CurrentAttribute, otherwise E::TT::Assertion could pick up information if it was nested in a non-TT attribute validation. Ideally, I would like to be able to have code something like this: my ($type, $value, $attr_name, $var_name) = ...; my $type_var = '$isa_check_for_'.sanitize_identifier($attr_name); $captures->{$type_var} = \$type; $check->inline_attribute_check($value, $type_var, $attr_name, $var_name, 'isa check'); I'm can work on the implementation of this if we can agree on a suitable API.
On 2017-02-13T11:13:56Z, haarg wrote: Show quoted text
> I'm can work on the implementation of this if we can agree on a > suitable API.
The API you suggest looks fine to me. I'd be happy to accept a patch for it, provided what the parameters meant was documented in not-too-Moo-specific terminology. I'm hoping to release the next stable Type::Tiny in about April, which will be version 1.002000, and would be a suitable release to target this addition for.
In retrospect, instead of this: $check->inline_attribute_check($value, $type_var, $attr_name, $var_name, 'isa check'); I'd rather something like this: $check->inline_attribute_check($value_var, $type_var, \%extra_fields); This would make it more future-proof and less Moo-specific. As I said earlier, I'm happy to accept a patch for this. Would be nice to get it into 1.006000. Especially if it made MooX::TypeTiny small enough to be able to just push the rest into Moo directly removing the need for a shim module.
Just checked into the repo the following change: Error::TypeTiny::Assertion->throw( ..., mgaca => 0 ) This tells Error::TypeTiny::Assertion to ignore $Method::Generate::Accessor::CurrentAttribute, so should save you having to localize it in Type::Tiny 1.008000 and above. I'll be keeping this out of the pod because it's so obscure, but it is documented in the source code comments and is considered a supported part of the API. While I'm sure there's still some improvements and cleanups that can be done, avoiding local should have a measurable speed improvement.
fixed in 1.007_009
Fixed in Type::Tiny 1.007_xxx.