Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > New Scripting Engine (GS2)
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 01-23-2007, 12:15 AM
JkWhoSaysNi JkWhoSaysNi is offline
Ruler of the graalaxy
Join Date: Feb 2005
Location: Great Britain
Posts: 488
JkWhoSaysNi is on a distinguished road
Send a message via ICQ to JkWhoSaysNi
Ternary Operator question..

Why doesn't this work:

variable ? function() : function2();

yet this does:

x = variable ? function() : function2();

The first example never gets processed. It's easier and neater than using

if (var) {
function();
}
else {
function2();
}
__________________

Coming soon (Hopefully:P)
Reply With Quote
  #2  
Old 01-23-2007, 01:09 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by JkWhoSaysNi View Post
variable ? function() : function2();
You've written it as if it were some sort of function. This is like doing
1+1;
It's just an operator.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #3  
Old 01-23-2007, 01:21 AM
Falcor Falcor is offline
Darth Cucumber
Falcor's Avatar
Join Date: Mar 2001
Location: At School
Posts: 2,874
Falcor is on a distinguished road
Send a message via ICQ to Falcor Send a message via AIM to Falcor Send a message via MSN to Falcor Send a message via Yahoo to Falcor
Quote:
Originally Posted by JkWhoSaysNi View Post
Why doesn't this work:

variable ? function() : function2();

yet this does:

x = variable ? function() : function2();

The first example never gets processed. It's easier and neater than using

if (var) {
function();
}
else {
function2();
}
I recommend you don't use the ternary operator for anything aside from simple substitutions. eg... (x>1? 0 : 1)
Its a very bad programming practice to use it as flow-control. It's not intended for that.
__________________

subliminal message: 1+1=3
Reply With Quote
  #4  
Old 01-23-2007, 01:33 AM
JkWhoSaysNi JkWhoSaysNi is offline
Ruler of the graalaxy
Join Date: Feb 2005
Location: Great Britain
Posts: 488
JkWhoSaysNi is on a distinguished road
Send a message via ICQ to JkWhoSaysNi
Why? It's only like a 2 line case statement.

It's much neater than either a case statement or if / else and has the same result.
__________________

Coming soon (Hopefully:P)
Reply With Quote
  #5  
Old 01-23-2007, 01:52 AM
Falcor Falcor is offline
Darth Cucumber
Falcor's Avatar
Join Date: Mar 2001
Location: At School
Posts: 2,874
Falcor is on a distinguished road
Send a message via ICQ to Falcor Send a message via AIM to Falcor Send a message via MSN to Falcor Send a message via Yahoo to Falcor
Quote:
Originally Posted by JkWhoSaysNi View Post
Why? It's only like a 2 line case statement.

It's much neater than either a case statement or if / else and has the same result.
You sacrifice code clarity for a few less characters. if(condition) this; else that; makes more sense than (condition? this : that) when you glance at your code. You shouldn't have to comment code that documents itself either.

There is a reason why ?: is considered an operator and not flow control. Because it isn't.

In reference to your original question, the compiler probably skips over statements that don't make sense like 1+1; Why waste CPU resources on some stand alone expression whose computed value is never used? The assumption the compiler makes is that (?:) is used for expressions, not for program flow. To act otherwise is dangerous, especially if the nature of the compiler changes, it may break your code.
__________________

subliminal message: 1+1=3

Last edited by Falcor; 01-23-2007 at 01:53 AM.. Reason: smiles.
Reply With Quote
  #6  
Old 01-23-2007, 03:29 PM
Admins Admins is offline
Graal Administration
Join Date: Jan 2000
Location: Admins
Posts: 11,693
Admins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud of
Hmmm The compiler was skipping the operation, although it should may be changed so that it is more compatible to C/C++.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 06:43 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.