B4J Question B4J v9.10 BETA is available for download

Erel

B4X founder
Staff member
Licensed User
Longtime User
It has been a while since the B4X language itself received new features. This update adds two new language features:

- IIf - Inline If, also called ternary if as it is an operator with three arguments.
B4X:
Label1.Text = IIf(EditText1.Text <> "", EditText1.Text, "Please enter value")
IIf is mostly equivalent to this sub:
B4X:
Sub PseudoIIf (Condition As Boolean, TrueValue As Object, FalseValue As Object) As Object
 If Condition = True Then Return TrueValue Else Return FalseValue
End Sub
Unlike this sub, the IIf keyword will only evaluate the relevant expression. This means that this code will work properly:
B4X:
Return IIf(List1.Size > 0, List1.Get(0), "List is empty")

(There is another minor difference related to the return type. If it is set explicitly with the new As method, the compiler will avoid casting the values to Object and back to the target type. This is only significant in very tight and long loops).

- As - Inline casting. Allows inline casting from one type to another. Some examples:
B4X:
Dim Buttons As List = Array(Button1, Button2, Button3, Button4, Button5)
Dim s As String = Buttons.Get(2).As(B4XView).Text
Buttons.Get(2).As(B4XView).Text = "abc"
Dim j As String = $"{
data: {
key1: value1,
complex_key2: {key: value2}
},
items: [0, 1, 2]
}"$

Dim parser As JSONParser
parser.Initialize(j)
Dim m As Map = parser.NextObject
Dim value1 As String = m.Get("data").As(Map).Get("key1")
Dim value2 As String = m.Get("data").As(Map).Get("complex_key2").As(Map).Get("key")
Dim FirstItemInList As String = m.Get("items").As(List).Get(0)
For Each item As Int In m.Get("items").As(List)
    Log(item)
Next
Root.GetView(8).As(WebView).LoadUrl("https://www.google.com")

And:
B4X:
Button1.As(JavaObject).RunMethod("setMouseTransparent", Array(True))
It can also be used with numbers, which is especially useful when calling external APIs with JavaObject, as the types need to be exact:
B4X:
Log(Me.As(JavaObject).RunMethod("sum", Array((10).As(Float), (20).As(Double))))
'equivalent to:
Dim jme As JavaObject = Me
Dim f As Float = 10
Dim d As Double = 20
Log(jme.RunMethod("sum", Array(f, d)))

#if Java
public double sum(float n1, double n2) {
return n1 + n2;
}
#End If

This update also makes it possible to define custom class templates inside b4xlibs. You can see an example in B4XPages.b4xlib. With this feature, it will be possible to add B4XPages classes in B4i.
B4i update with these features and more will be released next week.

Download link:
 

JordiCP

Expert
Licensed User
Longtime User
IIf is built as an operator and it is not a complete code block by itself. You can use 'single line if' in such cases instead.
Agree, thanks. Incredibly, that's one of those things that I just realised now, after so many years! ?

For whatever reason, I always assumed (and that's why I never tried it) that single-line If was limited to the True case
B4X:
If condiiton Then doThis
, thinking that I had to write the full 5-line expression for the if-then-else-endif for the complete case, not being aware that I could also use the single-line IIf
B4X:
If condition Then doThis Else doThat
 
Upvote 0

chikega

Member
Licensed User
Great addition to a great language, Erel!??

This schematic helped me to understand the flow of a ternary if (albeit in Java) ? Hopefully it will help others...
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Although I really really value the addition of inline casting, the ternary has always been less than transparent for me. Others obviously feel different.

Even though it is longer, I think I'll keep on using:

B4X:
If expression1 then
    expression2
Else
    expression3
End If
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
B4X:
' B4J.
Private Sub MyLabel_MouseClicked (EventData As MouseEvent)
   Dim Sign as int = IIf(EventData.PrimaryButtonPressed, 1, - 1)


' Used to increase/decrease a value on left/right mouse button click.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
hi! found another issue or may be i am wrong in the syntax.
How do i make an inline casting to an array?
I forgot to write it, but at least in the first version, 'As' doesn't support casting to arrays. I will add a proper error message for this case.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Me too. Almost forgotten and I remembered back when I tried to simplify my code for FormatLakh code snippet. The inline if-then-else works for one line of code but if we have more lines then we have to use the Else on next 2nd or 3rd line.
 
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
Thank you Erel, for IIF. I had no idea something so simple could generate so much commentary. No wonder you were so hesitant to add it!
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
That's right, more compact code, more visible

Missing usefull things as well :

+ in string concatenation ( in addition of &)

+= , -= , *= and /= with =+, =-, =* and =/

++ and --

Regards

Patrick
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
+ in string concatenation ( in addition of &)
That won't work as B4X is weakly typed and does automatic string conversions. The & operator is a flag to do a string concatenation and not a numeric addition .

Personally I'm also happy without the others. I use them in Java, C and C# but I'm quite happy doing them longhand in B4X - less cryptic, more readable, more in keeping with the simplicity of the language. I'd live without IFF as well, although I habitually use X ? Y : Z in those other languages.
 
Last edited:
Upvote 0

Heuristx

Active Member
Licensed User
Longtime User
That's right, more compact code, more visible

Missing usefull things as well :

+ in string concatenation ( in addition of &)

+= , -= , *= and /= with =+, =-, =* and =/

++ and --

Regards

Patrick

The "B" in B4X stands for BASIC.
 
Upvote 0

Heuristx

Active Member
Licensed User
Longtime User
"We" should change it to "Best", so the presumptuous critics will not be able to cling to this fact.


I hope it is clear that I meant that BASIC is a positive thing all by itself, I guess that is why we are here in the B4X camp.

In the 1920's an inventor approached a writer to ask him to endorse his newest invention: the perfect artificial language. He said the problem with natural languages was the vocabulary, it is difficult to memorize thousands of words. So he found the solution: instead of words, we write down whole sentences with simple symbols. These sentences will mean the same in every national language, so naturally, it will be easier to learn the language. It is an elegant and perfect solution to the problem of international communication.

The writer asked the inventor to illustrate his idea with an example. The inventor wrote a plus sign and drew a circle around it. When asked what it meant, he proudly announced: "Something is moving in the tree on the left, it may be a bat."
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
Upvote 0

Heuristx

Active Member
Licensed User
Longtime User
Sure. Basic has many dialects and anyone can choose to implement anything. Luckily the decision of what and how to add to B4X is in the hands of Erel, and I think he shows very good judgement.
The traditional syntax in Basic is Inc(x) and Dec(x) for incrementing and decrementing(I guess it comes from assemblers?) It is very simple to write a Sub for them. You can add Inc2(x, y) and Dec2(x, y), too, in B4X style.
The strength of Basic is that it is very close to human language and so it looks natural to the eye and easy to scan quickly. The added redundancy of spelled-out words makes it more difficult to make mistakes, too.
Have you ever poured over some complex line of code figuring out where the parentheses should be? I sure have. Yet actual words don't give us this kind of confusion.
 
Upvote 0

Heuristx

Active Member
Licensed User
Longtime User
I thought B4J meant "Basic for Java", since the language syntax IS Basic. B4A is Basic for Android, and so on. B4X means Basic for anything. But it should be simple to find it out, Erel will know the answer to a great degree of certainty.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…