Subject: | inline_check code generation flaw/bug |
Date: | Sun, 05 Oct 2014 12:05:20 +0200 |
To: | bug-Type-Tiny [...] rt.cpan.org |
From: | mt1957 <mt1957 [...] gmail.com> |
Hi Toby,
Reading your blog 'Type::Tiny Tricks #4 Inlined Type Constraints' I've
seen a small bug in your generated code from *$type->inline_check()*,
where $type is from ArrayRef[Int], which I have tested to see if it
wasn't a writing error. This bug does not give a wrong result but it
takes more time than needed.
Code run;
*
**use Modern::Perl;**
**use Types::Standard -types;**
**
**my $type = ArrayRef[Int];**
**say $type->inline_check('$X');**
*
Generated code (beautified);
*do **
**{ ref($X) eq 'ARRAY'**
** and do**
** { my $ok = 1;**
** for my $i (@{$X})**
** { $ok = 0 && last**
** unless (defined $i and $i =~ /\A-?[0-9]+\z/);**
** };**
** $ok**
** }**
**}**
**
*The problem lies in '*$ok = 0 && last*'. The *and* operator will never
let *last* do its job because the result is already false hence let the
whole array be tested even when a wrong entry is already found.
A better solution would be '*($ok=0,last)*'. Saves an operation too I
might add :-).
Detail info;
Package: Type-Tiny-1.000004
Perl: perl5 (revision 5 version 16 subversion 3)
Os: Linux h03-fedora 3.14.17-100.fc19.x86_64 #1 SMP Thu Aug 14 17:17:26
UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
Greetings
Marcel