Programming > Bugs & Feature Requests
Removal of C-style Ternary
(1/1)
Charles Pegge:
O2 will no longer support C expressions like:
r=(a>b ? c : d)
This is an expensive construction to check for during compilation.
Basic ternary expressions are easily implemented using a macro:
--- Code: ---macro iif int(r, a ,b, c)
if a
r=b
else
r=c
endif
end macro
r=iif(a>b , c, d)
--- End code ---
correction: r=iif(a>b , c, d)
Nicola:
Please explain to me how this macro works?
thank you :-[
Charles Pegge:
--- Code: ---macro iif int(r, a ,b, c)
'int indicates the type of temp variable r
'r will replace the macro invoked in an expression
'the macro is executed beforehand to resolve the value of r
'a is a boolean / comparative expression
'b and c are return values for r
if a
r=b
else
r=c
endif
end macro
r=iif(a>b , c, d)
'expands to:
'int temp_r
'if a>b
' temp_r=c
'else
' temp_r=d
'endif
'...
'r=temp_r
--- End code ---
Nicola:
Ok Charles.
For me it is clearer in this way, using variables with different names, because I can better identify which are the variable parameters and which are part of the macro and which depend on the values passed through the call of the macro itself. In our case iif ...
Still, a really good thing. Good job.
--- Code: OxygenBasic ---use consolecls 'macro iif int(r, a ,b, c)macro iif int(r, x ,y, z) if x r=y else r=z endifend macro '==main==========int a=5, b=6, c=7, d=9 int t=iif(a>b , c, d)print twait
In this case, rightly so, the result is 9.
JRS:
Are macros like user defined syntax extensions?
How are macros unlike functions?
--- Code: Script BASIC ---'IIF Function FUNCTION IIF(x, y, z) IF x THEN IIF = y ELSE IIF = z END IFEND FUNCTION SPLIT "5,6,7,9" BY "," TO a, b, c, d PRINT IIF(a > b, c, d), "\n"
C:\ScriptBASIC\examples>sbc iif.sb
9
C:\ScriptBASIC\examples>
Navigation
[0] Message Index
Go to full version