News for hackers. Stuff that matters.
Re:“I'm basically giving myself a permanent vacation from be... by TooBrokeToBeg on Thursday July 12, 2018 @01:24PM (#17516742)
I'm still puzzled to why would a compiler allow assignment in an explicit conditional (outside of loop syntax). It's like a baked-in blindspot that most people just want to ignore for some reason. Some languages actually guard against this well enough (eg Kotlin) and say "don't". Even with guards in place, it's not all that complicated to work around in the edge cases where you might want to do it.
Re:“I'm basically giving myself a permanent vacation from be... by arkadiytehgraet on Thursday July 12, 2018 @02:02PM (#17517091)
Funny you should mention Kotlin; while I like the language a lot, I believe this particular feature would be of immense help in the following scenario:

Imagine you have a sealed class Foo, with `class Bar(x: String) : Foo()` and `Baz() : Foo()`.

Now, imagine you have a method, returning an object of type Foo: `fun foo(): Foo`

And you want to pattern match on the result of this method:

  when(foo()) {
      is Bar -> ...
      is Baz -> ...
  }
Now, the problem is: how do you access String field x in the first branch? The only way to do it now is to extract the methos call into redundant local variable, and then pattern match it instead of `foo()` directly as I did above.

Now imagine Kotlin had that feature; then we could just do the following:

  when(foo = foo()) {
    is Bar -> foo.x
    is Baz -> ...
  }
Re:“I'm basically giving myself a permanent vacation from be... by bjz_ on Thursday July 12, 2018 @11:15PM (#17520246)
In Rust and Haskell you can do:

    match foo() {
        Bar @ foo => foo.x,
        Baz => ...,
    }
Re:“I'm basically giving myself a permanent vacation from be... by bpicolo on Thursday July 12, 2018 @02:21PM (#17517234)
One option is match statements. Great way to make this sort of inline assignment unnecessary
Re:“I'm basically giving myself a permanent vacation from be... by arkadiytehgraet on Thursday July 12, 2018 @02:24PM (#17517267)
Could you please elaborate more on what you mean by match statements? Is it an already existing feature of Kotlin?
Re:“I'm basically giving myself a permanent vacation from be... by bpicolo on Thursday July 12, 2018 @02:26PM (#17517291)
Ah, I guess `when` is literally Kotlin's equivalent of match.

What I'm thinking of here is Rust's match statements, which do give you the ability to make use of those intermediary values by making use of Rust's enum type.

https://doc.rust-lang.org/book/second-edition/ch06-02-match....

Re:“I'm basically giving myself a permanent vacation from be... by arkadiytehgraet on Thursday July 12, 2018 @02:32PM (#17517337)
I see; I agree that proper pattern matching would indeed solve that as well, as e.g. Scala does.
Re:“I'm basically giving myself a permanent vacation from be... by shakna on Thursday July 12, 2018 @06:25PM (#17519127)
It's handy in C for guarding parts of the language, as you often have to check return values anyway.

    if(malloc(10000 * sizeof foo) == NULL) {
      // Error processing
    } // No need for an else branch here.
Re:“I'm basically giving myself a permanent vacation from be... by the-dude on Thursday July 12, 2018 @06:56PM (#17519309)
Missing assingment.
Re:“I'm basically giving myself a permanent vacation from be... by shakna on Thursday July 12, 2018 @07:34PM (#17519481)

    int success = NULL;

    if(success = malloc(...)) {
    }
    if(success = malloc(...)) {
    }
    if(success = malloc(...)) {
    }
Re:“I'm basically giving myself a permanent vacation from be... by slavik81 on Thursday July 12, 2018 @08:39PM (#17519737)
That will result in undefined behaviour if the value returned by malloc is greater than INT_MAX. If you really need an integer, use intptr_t. But, generally it makes more sense to just use a pointer.

    char* p = NULL;
    if (p = malloc(...)) {
      ...
    }
Re:“I'm basically giving myself a permanent vacation from be... by kelnos on Thursday July 12, 2018 @02:05PM (#17517115)
Because assignment is an expression. It has nothing specifically to do with the position being a conditional.

Conditionals need an expression, and assignment fits the bill, so it works.

Re:“I'm basically giving myself a permanent vacation from be... by TooBrokeToBeg on Thursday July 12, 2018 @06:36PM (#17519195)
> Because assignment is an expression. It has nothing specifically to do with the position being a conditional.

No, but it has a lot to do with the way humans write code. This is the source of the bug, not the logic. eg Why bother having whitespace that the compiler can't use (typically)? Human readability.

The concession to not take into account human failing, is pathological.

Re:“I'm basically giving myself a permanent vacation from be... by hinkley on Thursday July 12, 2018 @10:30PM (#17520103)
The other partial fix is to stop allowing truthy conditionals. Only allow Boolean values and the only time you it wrong is when you’re assigning to booleans.
Re:“I'm basically giving myself a permanent vacation from be... by KSteffensen on Thursday July 12, 2018 @03:58PM (#17518108)
Why is assignment an expression and not a statement? Why should an assignment evaluate to a value?
Re:“I'm basically giving myself a permanent vacation from be... by wool_gather on Thursday July 12, 2018 @09:30PM (#17519910)
Well, if you have the concept of Maybe/Optional, it's quite handy to have a conditional binding as control flow (as in Swift):

    guard let value = myOptional else {
        // Handle absence
    }
    // Use value
Or

    if let value = myOptional {
        // Use value
    }
Re:“I'm basically giving myself a permanent vacation from be... by emmelaich on Thursday July 12, 2018 @09:36PM (#17519933)
Read Tim Peter's essay on the end of the PEP 572.

It's very good.