Robert Klemme
2/13/2005 4:37:00 PM
"David A. Black" <dblack@wobblini.net> schrieb im Newsbeitrag
news:Pine.LNX.4.61.0502130707250.22113@wobblini...
> Hi --
>
> On Sun, 13 Feb 2005, Martin DeMello wrote:
>
>> David A. Black <dblack@wobblini.net> wrote:
>>
>>> (For clarity I've deleted the warning about using '=' in conditional.
>>> [Matz, can't we get rid of that warning?])
>>
>> I'd love per-warning suppressibility, but if not, I think this particular
>> one does more good than harm. (But then, I find the if (a = f(x)) idiom
>> rather ugly to begin with).
>
> I'm trying to figure out the pattern behind when it gets triggered or
> not:
>
> irb(main):002:0> while (x = 1); end
> (irb):2: warning: found = in conditional, should be ==
> irb(main):003:0> while (line = gets); end
> abc
> ^D
> => nil
> irb(main):004:0> if (m = /a/.match("b")); end
> => nil
> irb(main):005:0> if (m = /a/); end
> (irb):5: warning: found = in conditional, should be ==
> irb(main):006:0> if (m = "abc".replace("abc")); end
> => nil
> irb(main):007:0> if (m = "abc"); end
> (irb):7: warning: found = in conditional, should be ==
> => nil
>
> When the rhs is a literal, I guess? That at least vastly increases
> the chances of its being a case of a typo. Could it be that Matz is
> several steps ahead of me? :-)
:-))
Note also, that assignment in a conditional really only makes sense for
loops. With "if" and "unless" it's totally superfluous since you can always
do
a=x
if a then...
instead of
if a=x then...
And it's also more readable. So I would not mind if there was *always* a
warning for assignment if it's "if" or "unless". Just my 0.02 EUR though...
Whereas there is no easy equivalent replacement for
while ( line = gets ) ...
Typically, you would need two assignments which is somewhat ugly:
line = gets
while line
....
line = gets
end
Kind regards
robert