Download link: https://www.b4x.com/b4j.html
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.
IIf is mostly equivalent to this sub:
Unlike this sub, the IIf keyword will only evaluate the relevant expression. This means that this code will work properly:
(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:
And:
It can also be used with numbers, which is especially useful when calling external APIs with JavaObject, as the types need to be exact:
This update also makes it possible to define custom class templates inside b4xlibs. You can see an example in B4XPages.b4xlib.
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")
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
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))
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.
Last edited: