ferbfletcher

Single Equal Sign, Ambiguity

9 posts in this topic

Why does AutoIt use "=" for both compare and for assignment?

Consider the following:

$x = 4

While ($x = 5)
ExitLoop
Wend

MsgBog("","",$x)


Now, what is the contents of $x ?


Many languages have a difference between assignment (=) and comparision (==)

AutoIt, however, uses (=) for both.

So, that While statement above could be read as
"while the contents of $x is equal to 5"  ... this would be false because $x is 4
-or-
it could be read as 
"while $x is commanded to take the value of 5" ... which would always be true because $x can always be commanded to take the value of 5.

Then, AFTER the while loop, $x might be 4 or it might be 5 ... depending on whether the (=) was taken to mean "compare" or "assign".

Of course, I can run the script easily enough and see the results for myself ... but this really does show the ambiguity of the equal sign in AutoIt.

 

Share this post


Link to post
Share on other sites



It's not ambiguous. AutoIt doesn't, for instance, support the "assignment operation is an expression that returns the assigned value" thing. When used in an expression it tests two values for some sort of equality, and when in an assignment command it assigns a value.

You didn't show any ambiguity, but you showed to two distinct ways of using the equals-sign.

Don't get me wrong, when I haven't coded in AutoIt for a long time I do that wrong all the time too. It isn't all that obvious... :'(


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

ferbfletcher,

The = sign is nowhere ambiguous in AutoIt. All = sign in an expression is an assertion while all = sign separating a LHV (left hand variable) and an expression is an assignment.

Hence your example where the = sign is part of an expression is obviously an assertion. And this is a Good Thing™: look at how many zillion bugs C has called for by allowing = in tests, forcing the programmers to adopt the un-natural style if (3 == myvar) { ...

The use of an operator in distinct contexts is common in programming languages: for instance, C uses * to either mean either multiply or pointer, depending on context. Yet, noone barks.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks.  When I said "ambiguous", I meant the exact same code "$x=something" can mean 2 different things based on where it is located (inside an if statement or standalone) ... not that AutoIt executes it in an unpredictable way.  Coming from C and PHP, this single equal sign is slightly confusing ... yet very simple, at the same time.

Edited by ferbfletcher

Share this post


Link to post
Share on other sites

Did you run into trouble because you expected you could assign variables inside an expression? Even in languages that support it, it's not something you see very often -- and personally every time I see it, I wonder if that's not something that would be better split onto a separate line. I'm honestly curious how you came to this idea. In the future, if you're learning a new language, memorize the operators documentation on the first day.

If you came from a Basic-variant language (and others), you'd be surprised at the double equals sign. So that's a very weak argument. You are correct to assert that many popular languages today do opt for a double equals sign. Perhaps if we were to build AutoIt from scratch, we'd probably reconsider this choice. Making that change now would break all existing AutoIt code ever written and the cost/benefit of that is simply not good enough to warrant a change.

Keep in mind that creating a programming language isn't easy. There's always some place where your chosen paradigms don't blend as perfectly as you'd want. What is easy is poking holes in languages that have gone through the growing pains. A good example is that we all love to make fun of JavaScript but actually improving the language is really hard and requires years of hard work.

Share this post


Link to post
Share on other sites

Did you run into trouble because you expected you could assign variables inside an expression? Even in languages that support it, it's not something you see very often -- and personally every time I see it, I wonder if that's not something that would be better split onto a separate line. I'm honestly curious how you came to this idea. In the future, if you're learning a new language, memorize the operators documentation on the first day.

​In PHP I do this a lot:

if (($a = $result) !== false) {

}

 

Share this post


Link to post
Share on other sites

Likewise in C[--|++]. Yet we're talking about AutoIt and it uses a distinct semantics for both = and == than some other languages.

The best way to recover peace of mind is to accept it or leave it.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Thank you, everyone, for your input!

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now