LEAVE/CATCH/return weirdness

--Apple-Mail=_466EC187-A5AF-4C7E-97A4-7BB390917971
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=utf-8

Hi,

Let me start with a sample which resembles some real code in my project:

     sub bar ( $v ) {
         die "don't like it" if $v =3D=3D 3;
         LEAVE (1..2).map: { "ok" };
         "[" ~ $v ~ "]"
     }

     sub foo ( $v --> Str ) {
         my $str =3D bar( $v );
         CATCH {
             default {
                 return "=C2=ABoops=C2=BB";
             }
         }
         $str
     }

     sub foobar {
         return [~] (1..10).map: {
             foo( $_ );
         };
     }

     say foobar;

With this I get the error:

Type check failed for return value; expected Str but got Int (1)
  in sub foo at ./fail.p6 line 16
  in sub foobar at ./fail.p6 line 21
  in block <unit> at ./fail.p6 line 25

When LEAVE is commented out I get the expected output:

[1][2]=C2=ABoops=C2=BB[4][5][6][7][8][9][10]

Somehow LEAVE substitutes return value from CATCH with 1. What's even =
more confusing is that I have no idea where this 1 is coming from!

Moreover, in real code LEAVE belongs to a sub which is one level deeper =
down the stack and is been called from bar(). The effect is the same: I =
get 1 instead of =C2=ABoops=C2=BB. Further investigation pinned down =
this miraculous phenomenon to .map. Any other code within LEAVE, =
including 'for' loop, makes the whole thing to work as expected.

I'm strongly convinced that what is observed is a bug. But before =
reporting it I'd like to make sure that there is nothing about .map that =
I don't know about and that makes the code behave so strange.

Best regards,
Vadim Belman


--Apple-Mail=_466EC187-A5AF-4C7E-97A4-7BB390917971
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=utf-8

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; =
charset=3Dutf-8"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; line-break: after-white-space;" =
class=3D"">Hi,<div class=3D""><br class=3D""></div><div class=3D"">Let =
me start with a sample which resembles some real code in my =
project:</div><div class=3D""><br class=3D""></div><div class=3D""><div =
class=3D""><font face=3D"Andale Mono" class=3D"">&nbsp; &nbsp; &nbsp;sub =
bar ( $v ) {</font></div><div class=3D""><font face=3D"Andale Mono" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;die "don't like it" if $v =
=3D=3D 3;</font></div><div class=3D""><font face=3D"Andale Mono" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LEAVE (1..2).map: { "ok" =
};</font></div><div class=3D""><font face=3D"Andale Mono" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"[" ~ $v ~ =
"]"</font></div><div class=3D""><font face=3D"Andale Mono" =
class=3D"">&nbsp; &nbsp; &nbsp;}</font></div><div class=3D""><font =
face=3D"Andale Mono" class=3D""><br class=3D""></font></div><div =
class=3D""><font face=3D"Andale Mono" class=3D"">&nbsp; &nbsp; &nbsp;sub =
foo ( $v --&gt; Str ) {</font></div><div class=3D""><font face=3D"Andale =
Mono" class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;my $str =3D bar( $v =
);</font></div><div class=3D""><font face=3D"Andale Mono" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CATCH {</font></div><div =
class=3D""><font face=3D"Andale Mono" class=3D"">&nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp;default {</font></div><div class=3D""><font =
face=3D"Andale Mono" class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp;return "=C2=ABoops=C2=BB";</font></div><div =
class=3D""><font face=3D"Andale Mono" class=3D"">&nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp;}</font></div><div class=3D""><font =
face=3D"Andale Mono" class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;}</font></div><div class=3D""><font face=3D"Andale Mono" =
class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$str</font></div><div =
class=3D""><font face=3D"Andale Mono" class=3D"">&nbsp; &nbsp; =
&nbsp;}</font></div><div class=3D""><font face=3D"Andale Mono" =
class=3D""><br class=3D""></font></div><div class=3D""><font =
face=3D"Andale Mono" class=3D"">&nbsp; &nbsp; &nbsp;sub foobar =
{</font></div><div class=3D""><font face=3D"Andale Mono" class=3D"">&nbsp;=
 &nbsp; &nbsp; &nbsp; &nbsp;return [~] (1..10).map: {</font></div><div =
class=3D""><font face=3D"Andale Mono" class=3D"">&nbsp; &nbsp; &nbsp; =
&nbsp; &nbsp; &nbsp; &nbsp;foo( $_ );</font></div><div class=3D""><font =
face=3D"Andale Mono" class=3D"">&nbsp; &nbsp; &nbsp; &nbsp; =
&nbsp;};</font></div><div class=3D""><font face=3D"Andale Mono" =
class=3D"">&nbsp; &nbsp; &nbsp;}</font></div><div class=3D""><font =
face=3D"Andale Mono" class=3D""><br class=3D""></font></div><div =
class=3D""><font face=3D"Andale Mono" class=3D"">&nbsp; &nbsp; &nbsp;say =
foobar;</font></div></div><div class=3D""><br class=3D""></div><div =
class=3D"">With this I get the error:</div><div class=3D""><br =
class=3D""></div><div class=3D""><div class=3D""><font face=3D"Andale =
Mono" class=3D"">Type check failed for return value; expected Str but =
got Int (1)</font></div><div class=3D""><font face=3D"Andale Mono" =
class=3D"">&nbsp; in sub foo at ./fail.p6 line 16</font></div><div =
class=3D""><font face=3D"Andale Mono" class=3D"">&nbsp; in sub foobar at =
../fail.p6 line 21</font></div><div class=3D""><font face=3D"Andale Mono" =
class=3D"">&nbsp; in block &lt;unit&gt; at ./fail.p6 line =
25</font></div></div><div class=3D""><br class=3D""></div><div =
class=3D"">When LEAVE is commented out I get the expected =
output:</div><div class=3D""><br class=3D""></div><div =
class=3D"">[1][2]=C2=ABoops=C2=BB[4][5][6][7][8][9][10]</div><div =
class=3D""><br class=3D""></div><div class=3D"">Somehow LEAVE =
substitutes return value from CATCH with 1. What's even more confusing =
is that I have no idea where this 1 is coming from!</div><div =
class=3D""><br class=3D""></div><div class=3D"">Moreover, in real code =
LEAVE belongs to a sub which is one level deeper down the stack and is =
been called from bar(). The effect is the same: I get 1 instead of =
=C2=ABoops=C2=BB. Further investigation pinned down this miraculous =
phenomenon to .map. Any other code within LEAVE, including 'for' loop, =
makes the whole thing to work as expected.</div><div class=3D""><br =
class=3D""></div><div class=3D"">I'm strongly convinced that what is =
observed is a bug. But before reporting it I'd like to make sure that =
there is nothing about .map that I don't know about and that makes the =
code behave so strange.</div><div class=3D""><br class=3D""><div =
class=3D"">
<div class=3D"">Best regards,</div>Vadim Belman

</div>

<br class=3D""></div></body></html>=

--Apple-Mail=_466EC187-A5AF-4C7E-97A4-7BB390917971--
0
vrurg
12/3/2018 1:35:25 AM
perl.perl6.users 1110 articles. 0 followers. Follow

0 Replies
14 Views

Similar Articles

[PageSpeed] 23

Reply: