Please, explain me this miracle with ?: ternary operator Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Please, explain me this miracle with ?: ternary operator

17 Oct 2021, 09:09

I'm really confused.
AhkVersion = 1.1.33.10

Code: Select all

a:=10, 
a:= (5>0) ? ++ : --
MsgBox,   % "a = " a
That gives me a=blank (msgbox shows "a = ")

But! Any of these two blocks give me a=11 (msgbox shows "a = 11"):

Code: Select all

a:=10, 
uselessVar:= a:= (5>0) ? ++ : --
MsgBox,   % "a = " a 

a:=10, 
uselessVar:=555,  a:= (5>0) ? ++ : --
MsgBox,   % "a = " a
In the last case uselessVar=11 (=a), not 555!

Of course, this code gives me a=9:

Code: Select all

a:=10, 
uselessVar:= a:= false ? ++ : --
MsgBox,   % "a = " a
So the question is: why alone line a:= (5>0) ? ++ : -- doesn't work and returns a blank?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Please, explain me this miracle with ?: ternary operator

17 Oct 2021, 10:27

i see no miracles, only syntax errors
Rohwedder
Posts: 7627
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Please, explain me this miracle with ?: ternary operator

17 Oct 2021, 10:51

Hallo,
even without ternary operator:

Code: Select all

a := 10
q := q, b := a := ++
MsgBox,% "a = " a "`nb = " b
Apparently the syntax error a := ++ is interpreted as ++a
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Please, explain me this miracle with ?: ternary operator

17 Oct 2021, 16:59

swagfag wrote:i see no miracles, only syntax errors
Seriously? Don't you see anything strange?
Whether there are syntax errors or not, I would expect the same result in all cases.
Why do I get a=11 in some cases and a=blank in others?
Rohwedder wrote: Apparently the syntax error a := ++ is interpreted as ++a
Why isn't it interpreted this way when we run this code:

Code: Select all

a := 10
a := ++
MsgBox,% "a = " a
Again, I wouldn't expect any difference in interpretator's behavior in these two cases. Why does the difference here exist - that was my question.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Please, explain me this miracle with ?: ternary operator

17 Oct 2021, 17:12

yes, seriously. the script contains syntax errors(i realize v1 doesn't complain about it, but they're there nonetheless. the fact that it doesn't is unfortunate. it's flawed in that regard. v2 on the other hand would have complained), so asking why something behaves a certain way instead of in a different supposedly expected way is meaningless.

fix the syntax errors and the ambiguities will be resolved. if not, it is only then that u may start investigating why some things are like this and not like that
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Please, explain me this miracle with ?: ternary operator

17 Oct 2021, 18:09

swagfag wrote:
17 Oct 2021, 17:12
fix the syntax errors and the ambiguities will be resolved. if not, it is only then that u may start investigating why some things are like this and not like that
But it is interestiing for me anyway.
There are syntax errors. Ok. Autohotkey should complain. Ok. But it doesn't complain. Ok. What value should Autohotkey assign to var a if it doesn't complain? It should be a blank, I think!
But why do I get a=11 in some cases of in fact the same syntax error? Why isn't it a blank?

And lets think about adding this trick to the interpretator.
A lot of years ago constructions as i++ didn't exist in programing. They would be syntax errors. Why did people invent such constructions? They are convinient and logically safe.
What should I use now in AHK v1? Something like this:

Code: Select all

LongLongVeryLongVar := (5>0) ? LongLongVeryLongVar + 1 : LongLongVeryLongVar - 1 

; or this:
if (5>0)
	LongLongVeryLongVar++
else 
	LongLongVeryLongVar--
That obviously isn't convinient. I think, Autohotkey should permit LongLongVeryLongVar := (5>0) ? ++ : --, b/c it is convinient and logically clear and safe.
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Please, explain me this miracle with ?: ternary operator

17 Oct 2021, 18:22

Although this works: LongLongVeryLongVar += (5>0) ? 1 : - 1
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Please, explain me this miracle with ?: ternary operator  Topic is solved

17 Oct 2021, 18:24

again, ure asking meaningless question. it shouldnt assign it any value. it shouldnt even attempt to run anything. it should emit an error message telling u ur script contains syntax errors and to go fix them. but v1 is too far gone to have that implemented now(which is why v2 exists)

Code: Select all

LongLongVeryLongVar += (5>0) ? 1 : -1
there, valid code with no syntax errors
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Please, explain me this miracle with ?: ternary operator

17 Oct 2021, 18:36

The same example, funny :).
Ok, I understood you, thank you! Although it would be nice to see LongLongVeryLongVar := (5>0) ? ++ : -- legal too in v1.1.33.11.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 334 guests