• Print

Author Topic: Mimic the c-style ternary operator '?:' in Lua  (Read 10537 times)

0 Members and 1 Guest are viewing this topic.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Mimic the c-style ternary operator '?:' in Lua
« on: September 15, 2008, 03:58:25 am »
Howdy all, I've always missed the c-style ternary conditional operator in Lua, but I found an equivalent this summer! First, I'll explain what I'm talking about...

How many times have you run into a situation like the following?
Code: Lua
  1. local foo
  2. if some_condition then
  3.     foo = "You lost, foo"
  4. else
  5.     foo = "You won!"
  6. end

Seems silly to have 6 lines of code for something so simple, right? In C/C++, you have an operator that helps here. The format is ( conditional_to_check ? execute_if_true : execute_if_false ). Pretty simple, huh? So to do the code we had above in C/C++, we'd do this:

Code: C
  1. foo = some_condition ? "You lost, foo!" : "You won!";

6 lines of code in one with no clarity lost! A programmer's best friend. No joy from Lua since this isn't officially supported, but you can emulate the behavior using and-or. So, the code above becomes:

Code: Lua
  1. foo = some_condition and "You lost, foo!" or "You won!"

It's not nearly as clear as the C/C++ operator, but it works. Why? Since 'and' takes precedence, it evaluates like this: foo = (some_condition and "You lost, foo!") or "You won!". If some_condition evaluates to true, it reads the "You lost, foo!" as well. Since the and'ed condition is true, it has no need to evaluate the second half of the or, so it returns "You lost, foo!". If some_condition is false, it doesn't bother finishing the evaluation of the and'ed condition since it can't possibly be true now. It evaluates the second half of the or and returns it.

The catches of using this method:
  • The "You lost, foo!" portion of the statement must evaluate to true or it will return the code that should execute when the conditional is false.
  • Since it's not part of the standard language, it might be confusing. And please, make sure you only use this method in simple conditional cases.

There you have it. Yes, I realize that any die hard Python programmers here probably already knew this. Am I going to use this? Definitely not as often as I'd use the C/C++ operator since it's just so darn confusing to look at this in Lua, but when I have a case like I outlined above it will definitely be a nice thing to have. :)
Experiencing God's grace one day at a time.

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 934
  • Karma: 62
    • Dev-Solutions
Re: Mimic the c-style ternary operator '?:' in Lua
« Reply #1 on: September 15, 2008, 03:10:50 pm »
Code: [Select]
foo = some_condition and "You lost, foo!" or "You won!"    -This is an interesting find. I have always thought about trying something like this, but to save time debugging I went the old way.
Code: [Select]
foo = some_condition && "You lost, foo!" || "You won!"    -Through in Garrys additions to gmod lua instead, and it really is confusing to the eyes  :o
An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Mimic the c-style ternary operator '?:' in Lua
« Reply #2 on: September 15, 2008, 04:05:15 pm »
Nice, though I'd be afraid to use it at all.
1) Lua version would change, update would change the precedence
2) GLua would change precedence in a Gmod version update for no good reason.

(Guess which is most likely, first guess doesn't count)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Mimic the c-style ternary operator '?:' in Lua
« Reply #3 on: September 15, 2008, 04:15:07 pm »
Lua's not going to change the order of operations, this is a standard that's carried across all languages (or at least all good languages). And if garry tries to change it, he'll be shot. :P
Experiencing God's grace one day at a time.

  • Print