News for hackers. Stuff that matters.
Re:Yoda Conditions by dom96 on Friday February 17, 2017 @11:46AM (#13669597)
There is nothing wrong with using equals for assignment, the language designers just have to disallow assignments in if conditions.
Re:Yoda Conditions by tel on Friday February 17, 2017 @12:05PM (#13669799)
It's a little weird. lvalues and rvalues are different things so a biased symbol feels nice to me, <-, e.g.
Re:Yoda Conditions by junke on Friday February 17, 2017 @12:03PM (#13669773)
One more corner case, instead of designing orthogonal features.
Re:Yoda Conditions by kibwen on Friday February 17, 2017 @12:16PM (#13669907)
It's not a corner case if you do it right.

For example, in C the assignment operator evaluates to the value of the right-hand side expression (so `int x = (y = 2);` initializes `x` to 2). But in Rust, the assignment operator always evaluates to `()` (a.k.a. "unit", the empty tuple), and trust me, this is how you want the language to work in the presence of pervasive move semantics and single ownership. Furthermore, nothing in Rust is "truthy": if-expressions accept a boolean and nothing else (this is also, IMO, exactly what you want in every language, but I'm sure others will disagree :P ). So `if x = 2 {` is a compiler error in Rust because `()` can't be coerced to a boolean (to say nothing of the fact that this reassignment would probably be a compiler error anyway because most variables in Rust are immutable). This doesn't require any acrobatics, it's just the natural fallout of how the rest of the language works.

Re:Yoda Conditions by junke on Friday February 17, 2017 @12:21PM (#13669937)
Thanks, that's indeed a sensible way to do that.
Re:Yoda Conditions by kazinator on Friday February 17, 2017 @07:00PM (#13672780)
That's a cure worse than the disease. If I faced a false choice between = as an assignment operator or else accepting syntax that distinguishes statements and expressions, I'd just take the = operator.
Re:Yoda Conditions by nilved on Friday February 17, 2017 @03:33PM (#13671465)
There's nothing wrong with using assignment in if conditions. For example, `if let Some(thing) = computation_that_may_fail { ... }`.
Re:Yoda Conditions by kazinator on Friday February 17, 2017 @06:53PM (#13672732)
The operator there is let. The = is just a punctuator which indicates the initial value expression for the let.

Litmus test: if this was rendered into S-expressions, the let would stay, but the = would disappear.

Re:Yoda Conditions by nilved on Sunday February 19, 2017 @12:53PM (#13680862)
No, that's just because the example I used was Rust. In Ruby it would be `if user = User.find(..)` and the same goes for Go and plenty of other languages.
Re:Yoda Conditions by kazinator on Sunday February 19, 2017 @10:47PM (#13684125)
If user is being freshly bound, that isn't an assignment but an initialization.

If the construct is not allowed when user is already in scope, it reflects the designer's view that an assignment in a conditional isn't a good thing.

If the construct is allowed when user exists already, with no diagnostic, then it carries the pitfalls that this thread is about.