Author Topic: Ternary operator in OxygenBasic?  (Read 3456 times)

0 Members and 1 Guest are viewing this topic.

Arnold

  • Guest
Ternary operator in OxygenBasic?
« on: March 18, 2016, 06:33:24 PM »
Hi Charles,

I am trying to use the functionality of a ternary operator in Oxygenbasic. This is the result I accomplished so far:

Code: [Select]
#include "$/inc/console.inc"

function iif(bool testVal, sys ifTrue, ifFalse) as sys
  if testVal then return ifTrue
  return ifFalse
end function
function iif(bool testVal, float ifTrue, ifFalse) as float
  if testVal then return ifTrue
  return ifFalse
end function
function iif(bool testVal, double ifTrue, ifFalse) as double
  if testVal then return ifTrue
  return ifFalse
end function
function iif(bool testVal, string ifTrue, ifFalse) as string
  if testVal then return ifTrue
  return ifFalse
end function

'Test
 
print "" iif((14==13), (14 * 15), "False") & cr    '( == )
'-----------------------

int A=5, B=6, C
c = iif(A > B, 100, 200)
print c & cr
'-----------------------

int a=1, b=2
string c
 
printl "a = 1  b = 2"
 
c = iif((a==b), "TRUE", "FALSE")            '( == )
printl "iif((a==b), TRUE, FALSE) returned " c
 
c = iif(a <> b, "TRUE", "FALSE")
printl "iif(a <> b, TRUE, FALSE) returned " c
 
c = iif(a < b, "TRUE", "FALSE")
printl "iif(a < b, TRUE, FALSE) returned " c
 
c = iif(a > b, "TRUE", "FALSE")
printl "iif(a > b, TRUE, FALSE) returned " c
'-----------------------

string buf1, buf2, str1
   
buf1 = "ABCDEFGHI"
buf2 = "ABCDEFGHI"
   
printl cr "buf1 = ABCDEFGHI  buf2 = ABCDEFGHI"
   
str1 = iif((buf1==buf2), "TRUE", "FALSE")   '( == )
if str1 = "TRUE" then               
   printl "buf1 is equal buf2" 
elseif str1 = "FALSE" then
   printl "buf1 is not equal buf2"
end if
'-----------------------

double hour = 12.30
string greeting = "Good" + iif((hour > 17), " evening.", " day.")  '( > )
printl cr greeting
'-----------------------

printl "Enter ... " : waitkey

When using the iif function it seems to be necessary to use braces for the condition if literal numbers are used and it is necessary to use braces and == for equality check.

Although my examples work, perhaps it is possible to use a more general method? In OxygenBasic\tests\Constructs I found iff.o2bas but it's iff function did not work for my tests.

Roland


.
« Last Edit: March 19, 2016, 12:15:27 AM by Arnold »

Charles Pegge

  • Guest
Re: Ternary operator in OxygenBasic?
« Reply #1 on: March 19, 2016, 02:56:21 AM »

Hi Roland

A macro might help with the polymorphic functions:

Code: OxygenBasic
  1. macro iifType(typ)
  2.   function iif(bool testVal, typ ifTrue, ifFalse) as typ
  3.     if testVal then return ifTrue
  4.     return ifFalse
  5.   end function
  6. end macro
  7.  
  8. iifType sys
  9. iifType float
  10. iifType double
  11. iifType string
  12.  
  13. 'test
  14.  
  15. r=""
  16. s="A"
  17. t="B"
  18.  
  19. r = iif ( (s==t), "true","false" )
  20. printl r
  21. waitkey
  22.  

Peter

  • Guest
Re: Ternary operator in OxygenBasic?
« Reply #2 on: March 19, 2016, 04:27:11 AM »
Hello,
Code: [Select]
macro iifType(typ)
function iif(bool testVal, typ ifTrue, ifFalse) as typ
    if testVal then return ifTrue
    return ifFalse
end function
end macro
 
iifType sys
iifType float
iifType double
iifType string
 
'test
 
r="A"
s="B"
t="B"
 
r = iif ( (s==t), "true","false" ) 'Okay
print r

r = iif ( (s=t), "true","false" )  'Okay
print r
 
r = iif ( (s==t), "true","false" ) '???
print r



Arnold

  • Guest
Re: Ternary operator in OxygenBasic?
« Reply #3 on: March 19, 2016, 09:37:54 AM »
Hi Charles,

thank you for this macro. It works very well for my purposes, moreover it is very instructive. (function inside a macro, returning different types)

@Peter: I get three times "true" as a result. But the second condition should be: (s==t). You should see the difference for (s="A") and (s=="A").

Roland

Charles Pegge

  • Guest
Re: Ternary operator in OxygenBasic?
« Reply #4 on: April 05, 2016, 12:43:55 AM »

Hi Roland,

In the latest release (1 April 2016), I reworked Variadic Macros. These facilitate even more compact code:

Code: OxygenBasic
  1. macro iifType(typ, ...)
  2.   function iif(bool testVal, typ ifTrue, ifFalse) as typ
  3.     if testVal then return ifTrue
  4.     return ifFalse
  5.   end function
  6. end macro
  7.  
  8. iifType sys,float,double,string
  9.  

Arnold

  • Guest
Re: Ternary operator in OxygenBasic?
« Reply #5 on: April 05, 2016, 12:42:43 PM »
Hi Charles,

I intend to use this macro in a major program of "Programming Windows" (Petzold) which I try to port to OxygenBasic and so far it works quite nice.

Working with the latest oxygen build since some days I do not discover differences in my current projects which of course do not tap the full potential of the language. I am not sure if there is a special reason for the same datestamp as with the oxygen.dll before?

Roland
« Last Edit: April 06, 2016, 06:45:39 AM by Arnold »

Charles Pegge

  • Guest
Re: Ternary operator in OxygenBasic?
« Reply #6 on: April 05, 2016, 11:01:17 PM »

Sorry, I'll make sure the datestamp is updated on the next release. This is done manually.

JRS

  • Guest
Re: Ternary operator in OxygenBasic?
« Reply #7 on: April 06, 2016, 12:41:04 AM »
I rarely rely on a date stamp to indicate change. I look at size first and if I'm still unsure I do a diff of the two. Just copying file to a new directory can change a date stamp.

Charles Pegge

  • Guest
Re: Ternary operator in OxygenBasic?
« Reply #8 on: April 06, 2016, 12:00:09 PM »
Hi John,

Roland is referring to the internal timestamp which forms part of the version string