 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10467
|
Posted: Wed Jan 10, 2007 2:49 pm Post subject: |
|
|
I don't want to guaranty left-to-right evaluation order in something like ++x + x*2 because I don't know of any popular languages that do so (if anyone knows of any, please let me know). Therefore, guarantying this in AutoHotkey might limit future flexibility.
In spite of this, I think the current evaluation order is unlikely to change. If it ever does change by accident (e.g. due to some optimization), the current battery of tests will alert me so that the change can be undone or documented in the changelog. |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5130 Location: eth0 ::1
|
Posted: Wed Jan 10, 2007 8:14 pm Post subject: |
|
|
| Chris wrote: | | I don't want to guaranty left-to-right evaluation order in something like ++x + x*2 | It should be the case if ahk follows php's operator precedence (javascript does). _________________
RegExReplace("irc.freenode.net/ahk", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2") |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4033 Location: Pittsburgh
|
Posted: Wed Jan 10, 2007 8:39 pm Post subject: |
|
|
Titan's references don't answer the question: how strongly is expression evaluation defined?
The interesting clause is for PHP: "Left associativity means that the expression is evaluated from left to right, right associativity means the opposite". I did not see anything, though, which guaranties that all terms are actually evaluated in expressions like "0 * x * y" or "0 && x && y". The problem is most obvious in the ternary operator: "x>1 ? --x : x" If x = 0, is --x evaluated? In most implementations it is not, but the left-to-right rule would mean the opposite. This is why Python is better: it does not allow these kind of side effects, which cause many bugs, allow bad coding style and save very little in the length of the code.
Javascript is similarly vague: in the referenced pages there is no mention of a possible short-circuit (aborted evaluation). About the ternary operation it only says: "If condition is true, the operator returns the value of expr1; otherwise, it returns the value of expr2." It is still not clear if expr1 is evaluated or not, when expr2 is returned. |
|
| Back to top |
|
 |
jonny
Joined: 13 Nov 2004 Posts: 3004 Location: Minnesota
|
Posted: Wed Jan 10, 2007 11:41 pm Post subject: |
|
|
| Referential transparency is only a problem in advanced, analytical programming. In almost every practical case, the behavior of side effects is well-known, as in using an assignment or increment in the middle of a purely mathematical expression. In cases where the behavior is in question, the same operation can be achieved a different way. For instance, "x>1 ? --x : x" could also be "x, x>1 && --x". |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5130 Location: eth0 ::1
|
Posted: Thu Jan 11, 2007 12:14 am Post subject: |
|
|
| Laszlo wrote: | | The problem is most obvious in the ternary operator: "x>1 ? --x : x" If x = 0, is --x evaluated? | The ternary operator is a statement (not arithmetic) which ignores --x if not x > 1 (but you already knew that!?). _________________
RegExReplace("irc.freenode.net/ahk", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2") |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4033 Location: Pittsburgh
|
Posted: Thu Jan 11, 2007 12:26 am Post subject: |
|
|
In many languages, like AHK, there is short circuit evaluation. It means, dependent on the value of expr1 in "expr1 && expr2", expr2 may or may not be evaluated. If it causes side effects, the result depends on the current value of expr1 *and* the SW version you use. There are compiler dependent optimizations in other languages, which might rearrange the evaluation order, or drop expressions, which don't affect the result. If possible, we ought to avoid using side effects, like x++ in expressions. Even if they work now, at a future release they might not. Simple code changes or unexpected values of sub expressions could have disastrous effects.
Titan: The ternary operator is a statement in some languages, but not in others. In AHK it is "operator" (a ternary one), returning a value, which can be used in the rest of the expression. In most languages it is not a statement, because it does not do anything with the computed value. |
|
| Back to top |
|
 |
jonny
Joined: 13 Nov 2004 Posts: 3004 Location: Minnesota
|
Posted: Thu Jan 11, 2007 1:46 am Post subject: |
|
|
| Quote: | | If possible, we ought to avoid using side effects, like x++ in expressions. Even if they work now, at a future release they might not. Simple code changes or unexpected values of sub expressions could have disastrous effects. |
I think this is being paranoid. Just because there's the possibility it could change at some future time doesn't mean we can't take advantage of the convenience it offers now. After all, you could say that about virtually any feature. Should we not use the Msgbox command where possible, because the name or syntax could change in future versions? What if it's phased out in favor of a function? Obviously it's too dangerous to use. |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4033 Location: Pittsburgh
|
Posted: Thu Jan 11, 2007 2:35 am Post subject: |
|
|
| jonny wrote: | | Just because there's the possibility it could change at some future time doesn't mean we can't take advantage of the convenience it offers now. After all, you could say that about virtually any feature. Should we not use the Msgbox command where possible, because the name or syntax could change in future versions? | You did not get it. We were speaking about undocumented features (order of expression evaluation). If an undocumented feature (you discover by trial an error) changes, it will not necessarily become documented. I can give you dozens of examples already seen in AHK. Just a couple: x+++x used to be the same as x:=2*x+1, now it is x:=x+1. z---z used to be the same z:=2*z-1, now it is z:=z-1. I don't believe you say it was very wise to use these, do you? |
|
| Back to top |
|
 |
jonny
Joined: 13 Nov 2004 Posts: 3004 Location: Minnesota
|
Posted: Thu Jan 11, 2007 3:56 am Post subject: |
|
|
Give me any practical scenario where it would be convenient to use x---x or x+++x and I'll concede to your argument. That syntax is ridiculous in any language.
Unless you have other examples of harmful and undocumented changes? |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4033 Location: Pittsburgh
|
Posted: Thu Jan 11, 2007 5:48 am Post subject: |
|
|
x+++x looks beautiful, does not use slow multiplication and 3 characters shorter than x:=2*x+1. I was tempted to use it myself, but I think, someone actually did (maybe Titan?). It is not about harmful or harmless changes, but using undocumented features. What is ridiculous to you may be OK for someone else.
This is another topic we are not going to agree on. I consider relying on undocumented features of the language to be dangerous and bad style, you consider it OK. My argument was that you might not see a change of undocumented feature documented, so it will be hard to figure out why your old script does not work in a new AHK release. You say (if I interpret it right) that this will never happen with anything practical. We just have to leave it there, and spend our time on something more productive.
Just for fun: What does the following script do? | Code: | MsgBox %][%
===
====
=====
:::::::: | It is actually useful, but relies on an undocumented feature. |
|
| Back to top |
|
 |
jonny
Joined: 13 Nov 2004 Posts: 3004 Location: Minnesota
|
Posted: Thu Jan 11, 2007 7:01 am Post subject: |
|
|
| Quote: | | x+++x looks beautiful, does not use slow multiplication and 3 characters shorter than x:=2*x+1. |
Precisely: it's obfuscated code. Creative, yes, just like the other script you posted, but not useful. x:=2*x+1 is by far more readable and intuitive.
| Quote: | | I consider relying on undocumented features of the language to be dangerous and bad style, you consider it OK. |
Stop putting words in my mouth. What I said was that ambiguous cases are rare and can be easily avoided, and that you're blowing this up into a bigger issue than it needs to be. So far, you're proving my point. |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4033 Location: Pittsburgh
|
Posted: Thu Jan 11, 2007 6:17 pm Post subject: |
|
|
| jonny wrote: | | …obfuscated code. Creative, yes, just like the other script you posted, but not useful. x:=2*x+1 is by far more readable and intuitive. | This was my point (you need not repeat it): don't use undocumented and/or obfuscated code.
| jonny wrote: | | ambiguous cases are rare and can be easily avoided | Again, my point was, that ambiguous cases should be avoided. Why do you repeat it? Do you want to express full agreement to my arguments? That's nice.
The other half of your sentence is false: ambiguous cases are NOT rare. Here are some constructs used many times in the scripts posted to the Forum.
@ ++empty: If y is empty, ++y alone makes y = 1, but x := ++y leaves y empty.
@ ++x + 2*x ; undocumented evaluation order
@ *(p++) + *p*256 ; undocumented evaluation order
@ x * ++y ; possible short circuit
@ x + ++y ; possible short circuit if x is empty
@ x && ++y ; documented short circuit
@ x>1 ? --x : x ; documented short circuit
@ The rules, when the precedence of an assignment is raised are undocumented
@ Precision of floating point numbers, conversion rules, comparisions are undocumented |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 6084
|
Posted: Thu Jan 11, 2007 6:47 pm Post subject: |
|
|
| Laszlo wrote: | Just for fun: What does the following script do? | Code: | MsgBox %][%
===
====
=====
:::::::: | It is actually useful, but relies on an undocumented feature. |
How does it run without an error!  |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4033 Location: Pittsburgh
|
Posted: Thu Jan 11, 2007 7:03 pm Post subject: |
|
|
| Skan wrote: | | How does it run without an error! | What do you mean? There is no error in the script. "][" is a valid variable name. The lines starting with "=" are continuation lines, merged to the previous one. The colons define a hotsring consisting of 5 colons, to be replaced by the empty string. That is, if you type ":::::" it will be deleted. It is undocumented, though, how many of the colons are treated as the hotstring (all but 4) and how many are treated as the replacement (none). |
|
| Back to top |
|
 |
jonny
Joined: 13 Nov 2004 Posts: 3004 Location: Minnesota
|
Posted: Thu Jan 11, 2007 7:07 pm Post subject: |
|
|
When an operator is the first thing on a line, it's considered a part of the line above it, to make expressions more readable:
| Code: | Msgbox,% 2 * 2
* 3 ; =12 |
Bad example I guess, but it helps for really long expressions. Anyway, the undocumented feature he refers to is that the continuation takes effect regardless of whether the preceding line ends with an expression or not.
Everything else in his script is fluff. This works the same:
Edit: Beat me to it. And I missed the hotstring. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|