PDA

View Full Version : Assignment in IF Clause


JustBreathe
06-22-2006, 11:26 AM
if ( temp.rid = true )
{
// Stuff
}


Does NOT work.


temp.rid = true;
if ( temp.rid )
{
// Stuff
}


DOES.

Wrapping assignment in brackets doesn't fix this problem.


Practical Usage:


function onCreated()
{
while ( temp.item = this.(@ "items"@ temp.count++ ) )
{
// Stuff
}
}

Loriel
06-22-2006, 12:00 PM
Does NOT work.

I believe this is for compatibility to GS1 where it was a feature for compatibility with Delphi-style comparisions.

JustBreathe
06-22-2006, 12:11 PM
It doesn't set anything at all. It totally ignores the assignment. I thought it was to prevent those who do if ( player.chat = "command" ) stuff;, but... Just because they have no notion of what an assignment operator does, doesn't mean that we should follow suit and make it into a comparison operator when those who do know have, now, no way of using the assignment operator.

Skyld
06-22-2006, 12:13 PM
It doesn't set anything at all. It totally ignores the assignment. I thought it was to prevent those who do if ( player.chat = "command" ) stuff;, but... Just because they have no notion of what an assignment operator does, doesn't mean that we should follow suit and make it into a comparison operator when those who do know have, now, no way of using the assignment operator.
What exactly are you expecting an assignment to return?

Rick
06-22-2006, 12:15 PM
GS2 automatically translates if (blah = thing) to if (blah == thing) to fix stupid people.

JustBreathe
06-22-2006, 12:18 PM
What exactly are you expecting an assignment to return?

temp.var = temp.var2 = temp.var3 = true;

is the same as

temp.var = ( temp.var2 = ( temp.var3 = true ) );
temp.var = ( temp.var2 = true );
temp.var = ( true );

if ( temp.var )... Temp.var would return true.

Skyld
06-22-2006, 12:19 PM
temp.var = temp.var2 = temp.var3 = true;

is the same as

temp.var = ( temp.var2 = ( temp.var3 = true ) );
temp.var = ( temp.var2 = true );
temp.var = ( true );

if ( temp.var )... Temp.var would return true.
So if you want it to return the value of the variable that you are assigning to, why did you not just say so?

JustBreathe
06-22-2006, 12:22 PM
So if you want it to return the value of the variable that you are assigning to, why did you not just say so?

If you used it in other languages, as does the person I was addressing the problem to has probably done, you would have understood.

Admins
06-22-2006, 02:14 PM
There are tons of scripts that the use wrong assignment operator, that's why it's better to keep this. It's not easy to debug either. Eventually it could display a warning when compiling the script and detecting an assignment operator inside an if/while/do statement. I would recommend to put the assignment into a separate line, that is better for readability anyway.

JustBreathe
06-22-2006, 02:18 PM
Could you have a server option that prevents GS1 backward compatibility.

I think leaving it as such is just encouraging bad standardization. Such as IE's CSS: The bad standardization impairs improvement of their CSS.

Admins
06-22-2006, 02:37 PM
Could be a server option yes

Twinny
06-22-2006, 02:50 PM
I would love for that to be a server option.

Rick
06-22-2006, 02:51 PM
I would love for that to be a server option.I'm suprised Stefan is going with that now :)

I'd also be happy to see this, even though I'm not much into dev anymore.

ApothiX
06-22-2006, 07:34 PM
So if you want it to return the value of the variable that you are assigning to, why did you not just say so?
It has practical applications mainly with functions that return false on error. Instead of having to have two separate lines you can do it all in one.

ie:

// ..
if(!(temp.blah = Foo(this.bar))) {
echo("ZOMG ERROR");
}
// ..
function Foo(bar) {
if(bar < 10) return false;
else return bar % 10;
}