[Wish] Inline Operators

Roger Garstang

Well-Known Member
Licensed User
Longtime User
The closer we get to OO the more can be done inline. One thing I like in other languages/environments is commands that can be done inline. In BASIC it is sometimes called Inline IF of IIF...C++/C# calls it various things but we used to call it the Huh? Operator because it uses a ? like- (BooleanTest) ? ReturnIfTrue : ReturnIfFalse. C# even has a ?? operator which is cool that test for null like X = Y ?? 2 Where X is assigned Y unless Y is null then it is set to 2.
 

imbault

Well-Known Member
Licensed User
Longtime User
wish operators

I do agree.

other operators like
:= or =
+= -= ...
also ++ --

would be great:
better to write

dim a:=0 as int

a++

than
dim a as int
a=0
a=a+1
 

barx

Well-Known Member
Licensed User
Longtime User
The java language does have such operator, it's caller the ternary conditional operator with the format of

B4X:
Val = (a > b)? a : b

How difficult it would be to bring this forward to b4a and the practicalities? I don't know?
 

bartv

Member
Licensed User
Longtime User
I support these requests.
As there is an equivalent in underlying java, the ternary operator could be implemented in B4A in the spirit of VB6 :
B4X:
Iif(condition, expr1, expr2)
 
Last edited:

wonder

Expert
Licensed User
Longtime User
IIF can be implemented this way:

@Erel, there is, however a BIG BIG problem with this code. Consider the following examples.

Java version:
B4X:
x = (abc > xyz) ? Read_The_First_10000_Records_And_Find_X(databaseName) : Read_The_Last_10000_Records_And_Find_X(databaseName);

// Result A:
// [abc] is evaluated against [xyz]
// [abc] is greater than [xyz], the first branch is executed:
//     -> Analyze ten thousand records in search of X
//     -> Get X
//     -> THE END

// Result B:
// [abc] is evaluated against [xyz]
// [abc] is less than or equal to [xyz], the second branch is executed:
//     -> Analyze ten thousand records in search of X
//     -> Get X
//     -> THE END

// Record search cost: Between 1 and 10,000 iterations.

B4x version:
B4X:
x = IFF(abc > xyz, Read_The_First_10000_Records_And_Find_X(databaseName), Read_The_Last_10000_Records_And_Find_X(databaseName))

'  Result A:
'  [abc] is evaluated against [xyz]
'      -> The result is passed as an argument to the IFF() method
'  The first ten thousand records are analyzed in search of X
'      -> The result is passed as an argument to the IFF() method
'  The last ten thousand records are analyzed in search of X
'      -> The result is passed as an argument to the IFF() method
'  In the IFF() method:
'  The first argument is TRUE:
'      -> Return the second argument
'      -> THE END

'  Result B:
'  [abc] is evaluated against [xyz]
'      -> The result is passed as an argument to the IFF() method
'  The first ten thousand records are analyzed in search of X
'      -> The result is passed as an argument to the IFF() method
'  The last ten thousand records are analyzed in search of X
'      -> The result is passed as an argument to the IFF() method
'  In the IFF() method:
'  The first argument is FALSE:
'      -> Return the third argument
'      -> THE END

'  Record search cost: Between 10,001 and 20,000 iterations.

I detected this problem in my own code, as I have been using the IFF() method for more than one year now...

Here's a practical crash/no crash example:
B4X:
Dim x(5) As Int
'The array x is populated...
...
...
...

Dim y As Int
Dim a = Rnd(0, 10) As Int
'----------------------------------------------------------------
'No crash (SAFE)
    If a < x.Length Then
        y = x(a)
    Else
        y = -1
    End If
'----------------------------------------------------------------
'Potential crash (UNSAFE)
    y = IFF(a < x.Length, x(a), -1) 'CRASH for any value of a > 4
'----------------------------------------------------------------

So yeah... we need the real thing, a real implementation of "condition ? branch_a : branch_b".
 
Last edited:
Top