Ternary bug or acceptable syntax? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

Ternary bug or acceptable syntax?

07 Feb 2019, 15:04

Normally when using ternary you'd use it to assign a value to a variable based on some condition, like this:

Code: Select all

x := 2
myVar := (x > 1) ? 101 : 404
You can also call functions the same way:

Code: Select all

x := 2
myVar := (x > 1) ? msgB() : msgB2()

msgB(){
	msgBox % "True"
}

msgB2(){
	msgBox % "False"
}
BUT this works too:

Code: Select all

x := 2
(x > 1) ? msgB() : msgB2()

msgB(){
	msgBox % "True"
}

msgB2(){
	msgBox % "False"
}
The above doesn't have variable assignment at all, so why is it working?
From what I know from other programming languages ternary can only be used with an assignment, yet here it's working fine without one?
Is this intended or is it some sort of a bug that shouldn't be there? If it shouldn't be there, would you recommend using it anyway or just stick to the normal usage of ternary?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Ternary bug or acceptable syntax?

07 Feb 2019, 15:24

yeah well, other languages allow single line if/else statements. its intended for v2, certainly
https://www.autohotkey.com/boards/viewtopic.php?p=195810#p195810
Fixed standalone ternary expressions like x ? ++y : --z. Specifically, those which start with a word and space followed by '?'.
https://www.autohotkey.com/boards/viewtopic.php?p=12074#p12074
To write a standalone ternary, post-increment or post-decrement expression, either omit the space between the variable and the operator, or wrap the variable or expression in parentheses.
https://lexikos.github.io/v2/docs/Language.htm#expression-statements
Ternary expressions such as x? CallIfTrue() : CallIfFalse().
although the docs seems to be referencing outdated information
teadrinker
Posts: 4349
Joined: 29 Mar 2015, 09:41
Contact:

Re: Ternary bug or acceptable syntax?

07 Feb 2019, 16:04

Cuadrix wrote:
07 Feb 2019, 15:04
From what I know from other programming languages ternary can only be used with an assignment
It's not true. Javascript:

Code: Select all

javascript = 
(
var bool = true;
bool ? CallIfTrue() : CallIfFalse();

function CallIfTrue()  { alert('true'); }
function CallIfFalse() { alert('false'); }
)

oDoc := ComObjCreate("htmlfile")
oDoc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
oDoc.parentWindow.eval(javascript)
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Ternary bug or acceptable syntax?  Topic is solved

07 Feb 2019, 16:12

I tested in C++ and it seems legit. Cheers.

Code: Select all

	//test ternary operator without assignment
	int x = 0;
	int y = 0;
	int z = 0;

	(x == 0) ? (y = 1) : (y = 2);
	std::cout << y << std::endl; //1
	(x != 0) ? (y = 1) : (y = 2);
	std::cout << y << std::endl; //2

	z = (x == 0) ? (y = 1) : (y = 2);
	std::cout << y << std::endl; //1
	z = (x != 0) ? (y = 1) : (y = 2);
	std::cout << y << std::endl; //2
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Ternary bug or acceptable syntax?

07 Feb 2019, 22:09

Perhaps a point of confusion is If-Else vs If-Then-Else.

The ternary is better thought of as If-Then-Else,

Code: Select all

(x > 1) ? msgB() : msgB2()

Code: Select all

"If"-> (x > 1) "Then"-> msgB() "Else"-> msgB2()
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: Ternary bug or acceptable syntax?

08 Feb 2019, 05:56

I also managed to use a function call in a non expected command like GuiControl.

I have a function that focus a control based on the first invalid value it finds. It is not based on a return value. It is rather a condition checker and the result is a direct GuiControl, Focus command called within the function.

Then what I did was to write:
GuiControl, focus, % ( Ready ? "CONFIRM_BUTTON" : FcsInvalidCtrl() )

I was really impressed that the function call is working without any trouble even inside a command that expects a control ID as the paramater value !!

In my opinion it was a practical thing to discover even if it doesnt make so much sens
User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

Re: Ternary bug or acceptable syntax?

08 Feb 2019, 10:43

SOTE wrote:
07 Feb 2019, 22:09
Perhaps a point of confusion is If-Else vs If-Then-Else.

The ternary is better thought of as If-Then-Else,

Code: Select all

(x > 1) ? msgB() : msgB2()

Code: Select all

"If"-> (x > 1) "Then"-> msgB() "Else"-> msgB2()
I don't see how it's confusing, though. Pretty simple stuff

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: KolaBorat, SomeGuyFromEu and 145 guests