Extend Bool() as a ternary operator?

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Extend Bool() as a ternary operator?

Post 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.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Extend Bool() as a ternary operator?

Post by kenmo »

+1

Though I would call it IIf(expr, true, false) not Bool.
https://en.wikipedia.org/wiki/IIf
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Extend Bool() as a ternary operator?

Post 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
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: Extend Bool() as a ternary operator?

Post by Justin »

Yes iif would be better. Since bool is a little similar just wondering if it would be easy to implement.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Extend Bool() as a ternary operator?

Post by davido »

+1 for IIf()
DE AA EB
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Extend Bool() as a ternary operator?

Post by RSBasic »

+1
Image
Image
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Extend Bool() as a ternary operator?

Post 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
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Extend Bool() as a ternary operator?

Post 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.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Extend Bool() as a ternary operator?

Post 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
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Extend Bool() as a ternary operator?

Post 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
Last edited by STARGÅTE on Sat Mar 05, 2016 10:34 pm, edited 1 time in total.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Extend Bool() as a ternary operator?

Post 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.
DontTalkToMe
Enthusiast
Enthusiast
Posts: 334
Joined: Mon Feb 04, 2013 5:28 pm

Re: Extend Bool() as a ternary operator?

Post 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.
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Extend Bool() as a ternary operator?

Post 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.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Extend Bool() as a ternary operator?

Post 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"), "")
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Extend Bool() as a ternary operator?

Post 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.
Post Reply