ternary if or iif

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
idle
Always Here
Always Here
Posts: 5844
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

ternary if or iif

Post by idle »

I'm not sure if this is planned to be done though it could be very handy to have a ternary if or iif function
while it can be done to some extent with macro's the cost of the procedure call is a bit of a performance hit
and would be really nice to have in native code for convince.

see http://www.purebasic.fr/english/viewtop ... 12&t=39843

Code: Select all

Procedure.s IIFEvals(expr,*y,*n)
If expr
 ProcedureReturn PeekS(*y)
Else
 ProcedureReturn PeekS(*n)
EndIf
EndProcedure

Procedure IIFEvali(expr,y,n)
If expr
  ProcedureReturn y
Else
  ProcedureReturn n
EndIf
EndProcedure

Procedure.f IIFEvalf(expr,y.f,n.f)
If expr
  ProcedureReturn y
Else
  ProcedureReturn n
EndIf
EndProcedure

Procedure.d IIFEvald(expr,y.d,n.d)
If expr
  ProcedureReturn y
Else
  ProcedureReturn n
EndIf
EndProcedure

Procedure IIFEvalq(expr,y.q,n.q)
If expr
  ProcedureReturn y
Else
  ProcedureReturn n
EndIf
EndProcedure

Macro IIFI(expr,y,n)
IIFEvali((Not(expr)),n,y)
EndMacro

Macro IIFF(expr,y,n)
IIFEvalf((Not(expr)),n,y)
EndMacro

Macro IIFD(expr,y,n)
IIFEvald((Not(expr)),n,y)
EndMacro

Macro IIFQ(expr,y,n)
IIFEvalf((Not(expr)),n,y)
EndMacro

Macro IIFS(expr,y,n)
IIFEvals((Not(expr)),n,y)
EndMacro

Macro IIFfn(expr,a,b)
 If expr : a : Else : b : EndIf  
EndMacro 

Procedure p2(a.s)
  MessageRequester("test",a)
EndProcedure
  

a=5
b=4
c=2
d=3
e.f = 5.6
f.f = 5.5 

IIFfn(5>4,p2("hello"),MessageRequester("test","no"))
Debug IIFS(5.3>5.4,@"yes",@"no")
Debug IIFI(a > b,200,100) 
Debug IIFF(e > f,200.1234567,100.1234567)
Debug IIFD(5.5 = 5.6,200.123456789012345,10.123456789012345)
Debug IIFQ(a > c,$FFFFFFFF,$EEEEEEEE)


lp = 100000000
st = GetTickCount_()
For a = 1 To lp
   x = IIFi(5.5 > 7.5,200,100)
Next   
et1.f = (GetTickCount_() - st) / lp
st = GetTickCount_()
For a = 1 To lp
   If 5.5 > 7.5
      x = 200
   Else
      x = 100     
   EndIf
Next     
et2.f = (GetTickCount_() - st) / lp


MessageRequester("test", "IIFs " + StrF(et1,6) + " if " + StrF(et2,6))
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: ternary if or iif

Post by luis »

Would be nice to have inline procedures, this would solve a lot of similar problems (uhm, not problems really, requests). Not exactly as a native statement but very close without the activation record overhead.

In any case, I would like to have IIF me too. :)
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: ternary if or iif

Post by Kaeru Gaman »

I'm sorry to disturb here, but Fred already announced a Boolean() directive for a future version.
since that would change things completely, asking for some IIF with those examples seems a bit aside the roadmap...
oh... and have a nice day.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Re: ternary if or iif

Post by Kale »

idle wrote:I'm not sure if this is planned to be done though it could be very handy to have a ternary if or iif function
while it can be done to some extent with macro's the cost of the procedure call is a bit of a performance hit
and would be really nice to have in native code for convince.

see http://www.purebasic.fr/english/viewtop ... 12&t=39843

Code: Select all

...horrible duplication and in need of OOP...
What's wrong with:

Code: Select all

MyVar.s = IsTrue ? "true" : "false"
--Kale

Image
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: ternary if or iif

Post by Kaeru Gaman »

Kale wrote:What's wrong with:

Code: Select all

MyVar.s = IsTrue ? "true" : "false"
it's not wrong, but it's not BASIC, either.
oh... and have a nice day.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Re: ternary if or iif

Post by Kale »

Kaeru Gaman wrote:
Kale wrote:What's wrong with:

Code: Select all

MyVar.s = IsTrue ? "true" : "false"
it's not wrong, but it's not BASIC, either.
Exactly! But it's a lot more easy to read than that code up there. Does any BASIC support condition expressions?
--Kale

Image
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: ternary if or iif

Post by Kaeru Gaman »

most BASICs support conditional expressions / Boolean Expressions.
I know it since '83 from C64-Basic.

... or do you mean a IIF function?
that isn't BASIC at all.
oh... and have a nice day.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Re: ternary if or iif

Post by Kale »

Kaeru Gaman wrote:most BASICs support conditional expressions / Boolean Expressions.
I know it since '83 from C64-Basic.
Conditional expressions are not found in any BASIC language apart from VB which has the IIF() function which apparently isn't a true conditional expression but just a function.

http://en.wikipedia.org/wiki/IIf

I honestly don't think IIF is needed in PB because you can use a normal IF. Also i'm sure you could code a nicer way of implementing it using pointers? :?:
--Kale

Image
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: ternary if or iif

Post by DoubleDutch »

Who really cares? - most of PureBasic isn't anything like the original BASIC dialects.

iff +1
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Re: ternary if or iif

Post by Kale »

DoubleDutch wrote:Who really cares? - most of PureBasic isn't anything like the original BASIC dialects.

iff +1
True, but i'm just arguing if you're going to implement this, do it as an operator NOT a function and make it simple and easy to use. Operators can be optimised more to create inline code.

Code: Select all

MyVar.s = IsTrue ? "true" : "false"
This is far easier to read and use than the code above.
--Kale

Image
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: ternary if or iif

Post by DoubleDutch »

I agree with you that it should be an operator, not a function.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
idle
Always Here
Always Here
Posts: 5844
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: ternary if or iif

Post by idle »

Kale wrote:

Code: Select all

...horrible duplication and in need of OOP...
OOP wouldn't help at all, there's a reason for the repetition: Typecasting and Speed.
If you can do better then please do more than blow.

The reason it's posted is to show why we need / want a Ternary IF
It can't really be done efficiently with the overhead of a procedure call, while the resultant code works out around ~2x slower
than the normal ifs it's not that bad considering but it can only really be done efficiently if it's added to the language.

I prefer the function look to it x= iff(a > b, "yes","No")

Although it's not necessary, some people like ternary ifs, so they can use them with function calls or whatever.

If CreateFile(0, "MacroTest.txt")
CloseFile(0)
Debug "Delete File : " + IFFs(DeleteFile("MacroTest.txt"),@"Yes",@"No")
EndIf
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Re: ternary if or iif

Post by Kale »

idle wrote:OOP wouldn't help at all, there's a reason for the repetition
I'm sorry but there's no reason at all for repetition.

And OOP would help a great deal here, it's called Polymorphism, one of the pillars of OOP.

http://en.wikipedia.org/wiki/Polymorphi ... rogramming

Like i wrote earlier, im sure theres a better way to approach this using pointers, instead of the repitition.
--Kale

Image
User avatar
idle
Always Here
Always Here
Posts: 5844
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: ternary if or iif

Post by idle »

While I understand your sentiment, OOP isn't really going to help! It's not supposed to be easy on your eyes, it's supposed to be fast!
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: ternary if or iif

Post by Foz »

Ok, that's enough. The arguments of OOP vs Procedural will carry on into the eons and beyond. I'm fairly sure that the civil war in heaven was over OOP vs Procedural in the first place as well. And no, I don't know which side chose which. Though the "Generic Object Delay" does lend itself to the heavenly side... and in the furnace of Hell resides the "Scheme, Assemble, Test, Announce: Now" train of thought. ;)

I agree that an OOP "variant" type would be useful for an IIF() function, so that there is one base type that holds all, and then it can be casted as required.

As this isn't going to happen in PB anytime soon, the idea of using pointers instead is also of merit, but I think could leads itself to being unnecessarily complicated - it just depends on how it is implemented.

However, I think that a true ternary operator would be far easier to read and write : +1 from me :D
Post Reply