On Mon, Mar 20, 2017 at 02:36:49PM +0100, Francesco Rivetti wrote:
> On 18. mars 2017 11:54, Elizabeth Mattijsen wrote:
>
> > if (my $x = frobnicate(42)) {
> > say $x
> > }
> [...]
> > if frobnicate(42) -> $x {
> > say $x
> > }
>
> which is way more elegant. Should this make it wise to have a compile time
> warning for the former then?
FWIW, the two snippets above are not exactly equivalent. The scope of $x in
the second version is limited to the block, while in the first version it
extends beyond the if statement.
$ cat a1
if (my $x = abs(42)) { say $x; }
say "$x again";
$ ./perl6 a1
42
42 again
$ cat a2
if abs(42) -> $y { say $y; }
say "$y again";
$ ./perl6 a2
===SORRY!=== Error while compiling /home/pmichaud/p6/rakudo/a2
Variable '$y' is not declared
at /home/pmichaud/p6/rakudo/a2:2
------> say "⏏$y again";
While it might be appropriate to have a warning on simple assignment (as long
as there's also a way to suppress the warning), I wouldn't want a warning on
initialization, as in
if (my $x = ...) { ... }
In this case, the "my" makes it pretty clear that the assignment is intentional
and not a typo.
Pm