ACSUtils Wiki

An ACS library for ZDoom-based ports

User Tools

Site Tools


new:ternary

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
new:ternary [2018/02/18 18:28] – created korshunnew:ternary [2018/02/18 18:31] (current) korshun
Line 1: Line 1:
 ====== Ternary operator in ACS ====== ====== Ternary operator in ACS ======
  
-[[ACC]] doesn't support the ternary operator '?:', but [[BCC]] and [[GDCC]] do.+[[ACC]] doesn't support the ternary operator '?:', but [[BCC]] and [[GDCC]] do. To emulate it in ACC, ACSUtils implements the following trivial function:
  
-To emulate it in ACCACSUtils implements the following trivial function:+''any cond(bool conditionany a, any b)''
  
-''any cond(bool condition, any whentrue, any whenfalse)'' +If ''condition'' is true, it returns a, otherwise it returns b.
- +
-If ''condition'' is true, function returns ''whentrue'', otherwise it retuns ''whenfalse''.+
  
 The only difference is that all arguments are evaluated before the function is called. This means that ''cond(x, f(), g())'' will call both ''f()'' and ''g()'', while ''x ? f() : g()'' will call only one of them. The only difference is that all arguments are evaluated before the function is called. This means that ''cond(x, f(), g())'' will call both ''f()'' and ''g()'', while ''x ? f() : g()'' will call only one of them.
 +
 +===== Examples =====
 +Without ''cond()'':
 +<code>
 +int color = CR_RED;
 +if (PlayerTeam() == TEAM_BLUE)
 +    color = CR_BLUE;
 +DrawSomething(color);
 +</code>
 +
 +With ''cond()'':
 +<code>
 +DrawSomething(cond(PlayerTeam() == TEAM_BLUE, CR_BLUE, CR_RED));
 +</code>
new/ternary.1518971308.txt.gz · Last modified: 2018/02/18 18:28 by korshun