Page 1 of 3

Extend Bool() as a ternary operator?

Posted: Sat Mar 05, 2016 6:06 pm
by Justin
I wonder if it could be possible to extend Bool() like this and keep it as a compiler directive:

Code: Select all

Bool(exp [, true_val, false_val])
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.
Like a ternary operator.

Re: Extend Bool() as a ternary operator?

Posted: Sat Mar 05, 2016 6:17 pm
by kenmo
+1

Though I would call it IIf(expr, true, false) not Bool.
https://en.wikipedia.org/wiki/IIf

Re: Extend Bool() as a ternary operator?

Posted: Sat Mar 05, 2016 9:20 pm
by Demivec
Justin wrote:I wonder if it could be possible to extend Bool() like this and keep it as a compiler directive:

Code: Select all

Bool(exp [, true_val, false_val])
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.
Like a ternary operator.
I agree with kenmo that if this is implemented it should be named IIf() and not Bool().

If implemented it would save using a multiplier when combining the results from several Bool().

Code: Select all

result = Bool(x >= 5) * 12 - Bool(x < 5) * 5
result = IIf(x >= 5, 12, 5) ;requested feature
+1

Re: Extend Bool() as a ternary operator?

Posted: Sat Mar 05, 2016 9:39 pm
by Justin
Yes iif would be better. Since bool is a little similar just wondering if it would be easy to implement.

Re: Extend Bool() as a ternary operator?

Posted: Sat Mar 05, 2016 9:47 pm
by davido
+1 for IIf()

Re: Extend Bool() as a ternary operator?

Posted: Sat Mar 05, 2016 9:49 pm
by RSBasic
+1

Re: Extend Bool() as a ternary operator?

Posted: Sat Mar 05, 2016 9:50 pm
by Danilo
Justin wrote:just wondering if it would be easy to implement.
It is, and many programming languages have it. See: ?: and Ternary operation

Re: Extend Bool() as a ternary operator?

Posted: Sat Mar 05, 2016 10:03 pm
by STARGÅTE
What is the advantage of an iff() in PureBasic?

Just a note from your link:
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.
The same "problem" will be in PureBasic.

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.

Re: Extend Bool() as a ternary operator?

Posted: Sat Mar 05, 2016 10:13 pm
by Danilo
STARGÅTE wrote:PureBasic evaluates his parameter always from the last parameter to the first parameter.
PB knows short-circuit evaluation, so that's not entirely true. PB does not always evaluate everything.

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.

Code: Select all

Define x = IIF(a > b, #True, #False)
vs.

Code: Select all

Define x
If a > b
    x = #True
Else
    x = #False
EndIf

Re: Extend Bool() as a ternary operator?

Posted: Sat Mar 05, 2016 10:33 pm
by STARGÅTE
Danilo wrote:
STARGÅTE wrote:PureBasic evaluates his parameter always from the last parameter to the first parameter.
PB knows short-circuit evaluation, so that's not entirely true. PB does not always evaluate everything.
Right, you mean the Or and And operator:

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

Re: Extend Bool() as a ternary operator?

Posted: Sat Mar 05, 2016 10:34 pm
by Demivec
STARGÅTE wrote:What is the advantage of an iff() in PureBasic?

Just a note from your link:
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.
The same "problem" will be in PureBasic.

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.
That is not something I had though of.

So to restate what you said, if you supply values for a false or true parameter then those will have to be evaluated and the results passed to the function before any expressions are evaluated?

If that is true that would probably negate any speed advantages.

Re: Extend Bool() as a ternary operator?

Posted: Sat Mar 05, 2016 10:39 pm
by DontTalkToMe
STARGÅTE wrote: 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.

You just need to implement it as it should be implemented.

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.

Code: Select all

Define x = IIF(a > b, #True, #False)
vs.

Code: Select all

Define x
If a > b
    x = #True
Else
    x = #False
EndIf
Exactly right.
Demivec wrote:If that is true that would probably negate any speed advantages.
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.
I find it cleaner, more concise, can be used more easily inside macros because it's a single expression, etc.

Re: Extend Bool() as a ternary operator?

Posted: Sat Mar 05, 2016 10:59 pm
by Danilo
The order of evaluation is very easy to built into any compiler, and should not be a problem.
The compiler knows a function and it's arguments, for example:

Code: Select all

function: name = f
          arguments =
              arg1: type = bool
                    name = b
              arg2: type = int
                    name = x
              arg3: type = int
                    name = y
The order of evaluation of the arguments does depend on specific settings.
That the compiler evaluates arg3, arg2, arg1 in that order is only because
the target platforms require the arguments in that order on the stack.
If the target platform would require parameters in reverse order on the stack,
PB would simply generate code in the order arg1, arg2, arg3.

Order of evaluation really shouldn't be the problem for any (multi-platform) compiler.

Re: Extend Bool() as a ternary operator?

Posted: Sat Mar 05, 2016 11:12 pm
by Lunasole
Well, the IIF() is absolutely required as it can be seen from many posts about it. It is simple nice thing w/o noticeable side-effects and I wonder why not to add it not as operator, but as built-in function to some library.

This function is not as widely used as other logical, the little overhead produced by such function call looks definitely better than code trashed with a temp variables and lot of IF/ENDIF.

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"), "")

Re: Extend Bool() as a ternary operator?

Posted: Thu Nov 10, 2016 5:12 am
by Mistrel
I would rather see support for the ternary operator. You can't really replace it with additional parameters to Bool() since you would have to canonicalize as a single return type; probably int.