RFE: throw an error on a single "="when used in an "if"

Dear Perl 6 Developers,

Request for Enhancement:

Would you consider throwing a compiler error on the following:

perl6 -e 'my $x=2;my $y=3; if $x = $y {say "yes";} else {say "no";}'
yes


It should have two == signs if it is followed by a {do something}

This is the correct way:

$ perl6 -e 'my $x=2;my $y=3; if $x == $y {say "yes";} else {say "no";}'

Many thanks,
-T

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Serious error.
All shortcuts have disappeared.
Screen. Mind. Both are blank.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
ToddAndMargo
3/18/2017 9:19:34 AM
perl.perl6.users 742 articles. 0 followers. Follow

5 Replies
29 Views

Similar Articles

[PageSpeed] 11

Hi,
This is the same behaviour you can find in perl5 and is not a flaw. It is 
an assignment just before testing. So $x gets the value of $y and then test 
which becomes True.

This can also be very covenient when e.g calling a sub, then assign to a 
value then test like below in a while;

while my $nchars = $mystring.chars { # do something with $nchars }

Marcel


On March 18, 2017 10:20:36 AM ToddAndMargo <ToddAndMargo@zoho.com> wrote:

> Dear Perl 6 Developers,
>
> Request for Enhancement:
>
> Would you consider throwing a compiler error on the following:
>
> perl6 -e 'my $x=2;my $y=3; if $x = $y {say "yes";} else {say "no";}'
> yes
>
>
> It should have two == signs if it is followed by a {do something}
>
> This is the correct way:
>
> $ perl6 -e 'my $x=2;my $y=3; if $x == $y {say "yes";} else {say "no";}'
>
> Many thanks,
> -T
>
> --
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Serious error.
> All shortcuts have disappeared.
> Screen. Mind. Both are blank.
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
mt1957
3/18/2017 10:47:02 AM
(This time to the list)

> On 18 Mar 2017, at 10:19, ToddAndMargo <ToddAndMargo@zoho.com> wrote:
> Request for Enhancement:
>=20
> Would you consider throwing a compiler error on the following:
>=20
> perl6 -e 'my $x=3D2;my $y=3D3; if $x =3D $y {say "yes";} else {say =
"no";}=E2=80=99
> yes

FWIW, I don=E2=80=99t think that will ever become a compile time error, =
but a warning may be in place.  Even although in Perl *5* this has been =
discussed at length and considered to be a feature rather than a bug or =
a trap.


> It should have two =3D=3D signs if it is followed by a {do something}
>=20
> This is the correct way:
>=20
> $ perl6 -e 'my $x=3D2;my $y=3D3; if $x =3D=3D $y {say "yes";} else =
{say "no";}'

In Perl 5 many people use the syntax to assign the value of a =
complicated expression to be useable inside the if statement:

 if (my $x =3D frobnicate(42)) {
     say $x
 }

In Perl 6, we have a different syntax for that, by using a pointy block:

 if frobnicate(42) -> $x {
     say $x
 }

Generally, in Perl 5 and Perl 6, when comparing against a constant, you =
could consider teaching yourself to always put the constant on the =
left-hand side:

 if 42 =3D=3D $x

Should you then make a mistake, it will at least be obvious at runtime:

 $ 6 'my $x =3D 42; say "same" if 42 =3D $x'
 Cannot modify an immutable Int

One could argue this should be spotted at compile time.


HTH,

Liz=
0
liz
3/18/2017 10:54:21 AM
On 18. mars 2017 11:54, Elizabeth Mattijsen wrote:
> In Perl 5 many people use the syntax to assign the value of a complicated expression to be useable inside the if statement:
>
>  if (my $x = frobnicate(42)) {
>      say $x
>  }

which is very handy and used many times, I completely agree.

But as you said:

> In Perl 6, we have a different syntax for that, by using a pointy block:
>
>  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?


> Generally, in Perl 5 and Perl 6, when comparing against a constant, you could consider teaching yourself to always put the constant on the left-hand side:

but that applies only when you compare with a constant. (And to be 
honest, sounds like a suggestion from the 80')

F
0
oha
3/20/2017 1:36:49 PM
On Mon, Mar 20, 2017 at 02:36:49PM +0100, Francesco Rivetti wrote:
> On 18. mars 2017 11:54, Elizabeth Mattijsen wrote:
>=20
> > if (my $x =3D frobnicate(42)) {
> >     say $x
> > }
> [...]
> > if frobnicate(42) -> $x {
> >     say $x
> > }
>=20
> which is way more elegant. Should this make it wise to have a compile t=
ime
> 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 versio=
n it extends beyond the if statement.

    $ cat a1
    if (my $x =3D 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
    =3D=3D=3DSORRY!=3D=3D=3D Error while compiling /home/pmichaud/p6/raku=
do/a2
    Variable '$y' is not declared
    at /home/pmichaud/p6/rakudo/a2:2
    ------> say "=E2=8F=8F$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 wa=
rning on initialization, as in

    if (my $x =3D ...) { ... }

In this case, the "my" makes it pretty clear that the assignment is inten=
tional and not a typo.=20

Pm
0
pmichaud
3/20/2017 2:54:30 PM
On 20. mars 2017 15:54, Patrick R. Michaud wrote:
> 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.

oh, in perl5 it is scoped inside the statement (but not garbage 
collected immediately after iirc)

>     if (my $x = ...) { ... }
>
> In this case, the "my" makes it pretty clear that the assignment is intentional and not a typo.

that would be lovely indeed

F
0
oha
3/21/2017 8:14:38 AM
Reply: