Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Hazarth94845y@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. -
@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.
-
Hazarth94845y@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. -
@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() )"
-
@saucyatom great example with using. But 'using' has an alternative, cleaner syntax now.
Related Rants
= != ==
rant
joke/meme