33
Comments
  • 2
    So, they did add code to have us killed. Damn it!
  • 3
    @asgs you know, just in case
  • 7
    tag =/= category
  • 1
    Is it about = or == ?
  • 2
    @R1100 yes.
  • 0
    @sp1dey but the compiler would probably count this as an error
  • 2
    @R1100 in case of C/C++, nope
  • 0
    @Lensflare not just c/c++. This is often valid as a successfull non-null assignment. Java does this to if you do something like
    if((line = readLine()) != Null){...}

    And this is somewhat common still!

    Java doesn't however implicitly cast to boolean nor allow int to be a truthy value (Python might work with this, I haven't tried that tho). So the above example would fail without the == null part

    However if readLine() in this example returned boolean it would work. Which is also true with the case in the image because its a boolean assignment!

    if(killAllHumans = true) {...} Is still evaluated as all around true and enters the IF statement just as in the pic!

    I haven't tried this with C# but I suspect it prolly works there too.
  • 1
    @Hazarth I'm not sure about c# too. Modern languages tend to avoid this pitfall though. All you need to get rid of is assignment expressions evaluating to the assigned value. Normally nobody needs that anyway.
  • 2
    @Lensflare I had to check to be sure, this is from the official C# documentation, last updated 06/21/2029:

    "...The result of an assignment expression is the value assigned to the left-hand operand..."

    I don't have a C# environment setup so if you or someone could confirm It's true then I could be sure. But this is at least what the docs say.
  • 1
    @Hazarth Not sure about 2029, but currently it is like in Java: "if ( ( a = _b() ) == c )" definitely works and there is no implicit cast to bool, thus leading to a compiler error if the result of whole expression is of another type. For conditionals this can lead to the stated problems, but it's great for using-blocks: "using (new IDisposable a = _b() )"
  • 0
    @saucyatom great example with using. But 'using' has an alternative, cleaner syntax now.
Add Comment