New feature - inline casting

Daestrum

Expert
Licensed User
Longtime User
I have used similar code for a while (adapted to use Erels example)

B4X:
    asB4XView(Buttons.Get(0)).Text = "hello"

...
Sub asB4XView(o As Object) As B4XView
    Return o
End Sub
 

Heuristx

Active Member
Licensed User
Longtime User
(MyClass)lst.Get(i)

is C-like syntax. The charm and strength of B4X is that it shields us from weird c-like expressions.
 

Heuristx

Active Member
Licensed User
Longtime User
It doesn't matter. What's wrong with (something) is:
parentheses are there to GROUP, that is, bunch things together, like (x + 3). x + (3) does not make sense. When you cast a value to a type, you are connecting the type with the value. So logically both should be inside the parentheses. When you isolate one element like (string), you visually isolate the type from the rest of the expression. This is linguistically unmotivated and illogical. To me, just plain annoying.
 

Heuristx

Active Member
Licensed User
Longtime User
That is your opinion, which you are entitled to. I like Erel's way much better and find it more logical.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Another example:
B4X:
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 As Map("data").Get("key1")
Dim value2 As String = m.Get As Map("data").Get As Map("complex_key2").Get("key")
Dim FirstItemInList As String = m.Get As List("items").Get(0)

For Each item As Int In m.Get As List("items")
    Log(item)
Next
 

Sandman

Expert
Licensed User
Longtime User
Another example:
B4X:
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 As Map("data").Get("key1")
Dim value2 As String = m.Get As Map("data").Get As Map("complex_key2").Get("key")
Dim FirstItemInList As String = m.Get As List("items").Get(0)

For Each item As Int In m.Get As List("items")
    Log(item)
Next
I 've stared at the code for ten minutes and I just can't parse it.

Can somebody please help me and convert it to the form that Jordi used (that we know isn't correct, but very simple to understand)? He did it like this:
buttons.Get(2).CastTo(B4XView).Text = "less boring"
 

Sandman

Expert
Licensed User
Longtime User
If you turned this...
B4X:
Dim value1 As String = m.Get As Map("data").Get("key1")

...to this, I would find it easier to understand
B4X:
Dim value1 As String = (m.Get("data") As Map).Get("key1")

...or something like this
B4X:
Dim value1 As String = ((Map) m.Get("data")).Get("key1")

...or something like this
B4X:
Dim value1 As String = m.Get("data").CastTo(Map).Get("key1")
 

alwaysbusy

Expert
Licensed User
Longtime User
And this:
B4X:
dim btn5 as B4XView = Buttons.Get(5)
dim btn2 as B4XView = Buttons.Get(2)
btn5.Text = btn2.Text
Buttons.Set(5,btn5) 'not really needed as btn5 already points to the instance in the list, but for arguments sake.

Would become this (first = Set):
B4X:
Buttons.Set As B4XView(5).Text = Buttons.Get As B4XView(2).Text

Or this (first = Get)?
B4X:
Buttons.Get As B4XView(5).Text = Buttons.Get As B4XView(2).Text
 

AnandGupta

Expert
Licensed User
Longtime User
Hmm.. Erel's implementation is simple and complex.

Once you get it, it becomes simple and will flow from your fingers. Till then it is complex to even read it.
The older members like me can use the older existing code flow, no problem there.

Let the new members see and behold the power of B4X !
 
Top