Skip Menu |

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

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

People
Owner: perl [...] toby.ink
Requestors: TIMB [...] cpan.org
Cc:
AdminCc:

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



Subject: Reduce boilerplate for inline_as
Instead of: inline_as { my ($constraint, $varname) = @_; my $perlcode = $constraint->parent->inline_check($varname) . "&& ($varname >= -32768 && $varname <= 32767)"; return $perlcode; }, I'd really like to avoid all that boilerplate and be able to write something like: inline_as { return (undef, "$_ >= -32768 && $_ <= 32767"); }, This build on three things: 1: allow returning a list of strings that will be merged via: join "&&", map { "($_)" } to form the inline condition 2: allow an initial undef to mean "whatever $constraint->parent->inline_check($varname)" returns 3: call the inline_as code with $_ set to $varname
On 2013-07-12T14:29:19+01:00, TIMB wrote: Show quoted text
> 3: call the inline_as code with $_ set to $varname
This bit should already work. Overall, I'd like to keep inline_as much as it is, because that's how inline_as works in Moose and Mouse. However, inline_as is just a shortcut for setting the "inlined" property of the declared type constraint; and there's nothing to rule out having more than one shortcut function.
That said, your suggestions could probably be implemented within the existing functions without breaking things.
Implemented this experimentally in 0.015_04. I will close this bug when a new stable release is available on CPAN.
resolved in 0.016
I'm puzzled by the "no guarantees that it will necessarily work with Moose/Mouse/Moo" in the updated docs. If undef is the same as $constraint->parent->inline_check($varname) shown in the example above, then why not flag that with the same qualifier in the docs? Or perhaps I'm misunderstanding what the qualifier relates to. Also no need for the outer parens in "($_ % 2 == 0)" as they're added by the code. Then the 10-line EvenInt example can be echoed by this equivalent 4-line version where the inline_as clause closely mirrors the where clause: declare EvenInt, as Int, where { $_ % 2 == 0 }, inline_as { (undef, "$_ % 2 == 0") }; What do you need to remove the "experimental" flag from this feature?
On 2013-07-18T15:41:53+01:00, TIMB wrote: Show quoted text
> I'm puzzled by the "no guarantees that it will necessarily work with > Moose/Mouse/Moo" in the updated docs.
If Moose/Mouse/Moo call $type->get_message($value), then it will work; no problem. I'm just worried that there might be some place in the Moose internals where it does something like: my $coderef = $type->inlined; my $message = $coderef->($value); And thus call the coderef directly, in scalar context, bypassing Type::Tiny::get_message, which is responsible for rewriting undef and joining the list with "&&". Grepping through the Moose source suggests not, but it's possible I've missed something; also there are myriad MooseX extensions that might. Show quoted text
> What do you need to remove the "experimental" flag from this feature?
Test cases. I'm unlikely to completely get rid of the feature, but if it turns out to be subtly broken because of above, it may need some implementation changes.
Show quoted text
> I'm just worried that there might be some place in the Moose internals > where it does something like: > > my $coderef = $type->inlined; > my $message = $coderef->($value); > > And thus call the coderef directly, in scalar context, bypassing > Type::Tiny::get_message, which is responsible for rewriting undef and > joining the list with "&&".
Could the rewriting undef and joining the list with "&&" be done earlier? Perhaps in the inline_as handler? Show quoted text
> > What do you need to remove the "experimental" flag from this feature?
> > Test cases.
Understood.
Show quoted text
> Could the rewriting undef and joining the list with "&&" be done > earlier? Perhaps in the inline_as handler?
There are basically three techniques: 1. Have get_message do the rewriting. This is what I've done for now as it seemed the least invasive technique. 2. Right now, inline_as basically just returns the exact coderef it's given as an argument. Instead it could return a wrapper around that coderef that does the rewriting. 3. Could rename the current $type->inlined method to $type->_real_inlined everywhere, and then create a new $type->inlined that returns a wrapper around the coderef returned by $type->_real_inlined. #2 means you only get the "return (undef, ...)" sugar if you use Type::Utils to declare your type constraints; you don't get it if you call Type::Tiny->new(...) directly. Also, #2 adds an extra sub call to each ->get_message, slowing it down a teeny bit. (Though ->get_message will not usually be in a "hot" code path in usual use cases.) #3 adds the extra sub call, but only for people who are calling ->inlined; people using ->get_message. So #3 is probably what I'll got for if #1 proves problematic. In fact, maybe even if it does not.