Code: Select all
Bool(exp [, true_val, false_val])
Like a ternary operator.
Code: Select all
Bool(exp [, true_val, false_val])
I agree with kenmo that if this is implemented it should be named IIf() and not Bool().Justin wrote:I wonder if it could be possible to extend Bool() like this and keep it as a compiler directive:If true_val and false_val are passed, true_val is returned if exp evaluates to true or false_val if evaluates to false. The values should be of any type.Code: Select all
Bool(exp [, true_val, false_val])
Like a ternary operator.
Code: Select all
result = Bool(x >= 5) * 12 - Bool(x < 5) * 5
result = IIf(x >= 5, 12, 5) ;requested feature
It is, and many programming languages have it. See: ?: and Ternary operationJustin wrote:just wondering if it would be easy to implement.
The same "problem" will be in PureBasic.Note that some languages may evaluate 'both' the true- and false-expressions, even though only one or the other will be assigned to the variable.
PB knows short-circuit evaluation, so that's not entirely true. PB does not always evaluate everything.STARGÅTE wrote:PureBasic evaluates his parameter always from the last parameter to the first parameter.
Code: Select all
Define x = IIF(a > b, #True, #False)
Code: Select all
Define x
If a > b
x = #True
Else
x = #False
EndIf
Right, you mean the Or and And operator:Danilo wrote:PB knows short-circuit evaluation, so that's not entirely true. PB does not always evaluate everything.STARGÅTE wrote:PureBasic evaluates his parameter always from the last parameter to the first parameter.
Code: Select all
Procedure.i MyDebug(Value)
Debug Value
ProcedureReturn Value
EndProcedure
Define A = Bool(MyDebug(1) = 1 Or MyDebug(2) = 2) ; Only MyDebug(1) is evaluated
Define B = Bool(MyDebug(3) = 0 And MyDebug(4) = 0) ; Only MyDebug(3) is evaluated
That is not something I had though of.STARGÅTE wrote:What is the advantage of an iff() in PureBasic?
Just a note from your link:The same "problem" will be in PureBasic.Note that some languages may evaluate 'both' the true- and false-expressions, even though only one or the other will be assigned to the variable.
PureBasic evaluates his parameter always from the last parameter to the first parameter.
A IIf(query, true, false) function, will evalue false, true and then query, whether query is true or false.
Then it wouldn't be IIF but another thing.STARGÅTE wrote: A IIf(query, true, false) function, will evalue false, true and then query, whether query is true or false.
Exactly right.Danilo wrote: With IIF, the first boolean comparison is executed, and depending on the result,
one of the two following expressions becomes the whole expression.
Just a shortcut to If..Else..EndIf, with the additional advantage that it can be used as an expression.vs.Code: Select all
Define x = IIF(a > b, #True, #False)
Code: Select all
Define x If a > b x = #True Else x = #False EndIf
I don't think this feature has nothing to do with speed, it's just a nicer way (for some, me included) to write that kind of expression on a single line instead of using if-then-else. It's a condition you encounter very often, and I would prefer to use that syntax, like in many other languages.Demivec wrote:If that is true that would probably negate any speed advantages.
Code: Select all
function: name = f
arguments =
arg1: type = bool
name = b
arg2: type = int
name = x
arg3: type = int
name = y
Code: Select all
Define S$ = IIF (expression, A, B) ; assuming IIF is implemented for return arguments of every base type. the compiler selects which implementation to use basing on type of A variable here (is returned if expression = true), B has to be the same type
Define S2$ = IIF( Bool(IIF (expression, 100, 200) = 100), "100", "something else") ; something nested
MessageRequester (IIF (expression, "str1", "str2"), "")