Page 1 of 1
Trinary Operator
Posted: Fri Sep 14, 2007 1:16 am
by kenmo
Any chance of adding a trinary operator to PB? Especially for setting variables. So instead of
Code: Select all
If Known : Name = HisName : Else : Name = "???" : Endif
you simply write
Though I know this may be a pain to implement into such a strongly structured language...
Posted: Fri Sep 14, 2007 9:14 am
by eesau
I like the idea, but I think that the C-like ternary operator ('?') is not so readable. To conform with the current PB-syntax, I'd like to have something more flowing, like natural language. Maybe Test/True/False -keywords, like this:
Code: Select all
Test Known And FurryAnimal True Name = "Peter Rabbit" False Name = "???"
Test JumpFlag True ProcedureThis ( ) False ProcedureThat ( )
Or using just either True or False keyword...
Test AtomicWar Or Godzilla Or Earthquake False Danger = 0
Test CountToZero True CountToZero - 1
Just my 0.02.
Posted: Fri Sep 14, 2007 9:24 am
by Kiffi
i think, the most readable construct is IIF():
Code: Select all
IIF(Expression, TruePart, FalsePart)
http://www.purebasic.fr/english/viewtopic.php?t=25788
Greetings ... Kiffi
Posted: Fri Sep 14, 2007 9:32 am
by eesau
Kiffi wrote:i think, the most readable construct is IIF()
Yes, it's somewhat readable, but it requires both TruePart and FalsePart, which I don't like. Makes the code longer.
Posted: Sat Sep 15, 2007 8:19 am
by kenmo
Yeah, IIF() is a nice macro but doesn't work for setting variables... I see netmaestro suggested the same thing as me at the bottom of the thread Kiffi linked to...
Posted: Sat Sep 15, 2007 5:16 pm
by #NULL
thats how i do it (for strings):
Code: Select all
Macro StrIf( _StrIf_condition_, _StrIf_string_IF_, _StrIf_string_ELSE_="")
_StrIf_proc( 0 Or (_StrIf_condition_), _StrIf_string_IF_, _StrIf_string_ELSE_ )
EndMacro
Procedure.s _StrIf_proc( condition.l, stringIF.s, stringELSE.s)
If condition
ProcedureReturn stringIF
Else
ProcedureReturn stringELSE
EndIf
EndProcedure
; Debug "---- EXAMPLE - StrIf.pbi"
; For i=0 To 10
; r=Random(20)-10
; Debug StrIf(r>0,"+") + Str(r)
; Next
; Debug "----"
; For i=0 To 10
; r=Random(3)
; Debug StrIf(Not r,"no", Str(r)) + StrIf(r>1," seats", " seat")
; Next
; Debug "----"