4 ms·
> There are subtle differences and some authorities prefer <- for assignment but I’m increasingly convinced that they are wrong; = is always safer. R doesn’t al
by cwyers 8y ago
> There are subtle differences and some authorities prefer <- for assignment but I’m increasingly convinced that they are wrong; = is always safer. R doesn’t allow expressions of the form if (a = testSomething()) but does allow if (a <- testSomething()).
...I am confused here. If you're testing for equality, R requires you to use == and not =. If you try to test for equality with =, it throws an error instead of treating it as an assignment. That's good. But who is trying to test for equality with <-?
- zaphar 8y agoHe's not testing for equality. He's assigning to a variable.
- cwyers 8y agoHe's saying to use = instead of <- because it won't let you assign to a variable there. But that's because it assumes you mistyped the equality operator. The only reason you need safety there is because it's easy to forget that == is the equality operator, not =. It's not easy to confuse == and <-.
- Wolog 8y agoThe danger is that you can intend to type if(b < -9) and instead type if(b <- 9) The latter always evaluates to true and assigns the value you're trying to compare with to your variable. This can be extremely difficult to catch and detect, especially for people who aren't software developers. They aren't writing unit tests.
- bo1024 8y agoI had the same confusion, can only assume the author is worried about this typo: if(a < -b) (which I assume is a less-than comparison, though I don't know R).
- joker3 8y agoIt is.