New feature - inline casting

alwaysbusy

Expert
Licensed User
Longtime User
if it is only one thing you need to change, it will have its value. However, if more properties have to be changed or be retrieved, the old way will be more useful I believe.

B4X:
log(Buttons.Get As B4XView(2).Text)
log(Buttons.Get As B4XView(2).Color)
log(Buttons.Get As B4XView(2).Top)
log(Buttons.Get As B4XView(2).Left)

 'vs
Dim btn as BX4View = Buttons.Get(2)
Log(btn.Text)
Log(btn.Color)
Log(btn.Top)
Log(btn.Left)
 

Heuristx

Active Member
Licensed User
Longtime User
Like in VB.

DirectCast(AnObject, AClass).AProperty = Value 'Erel's syntax is more elegant and BASIC-like, but that's a matter of personal preference.
Tiresome to write it many times, so there, too, we'd just assign it to a variable and go from there.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Would become this (first = Set):
B4X:
Buttons.Set As B4XView(5).Text = Buttons.Get As B4XView(2).Text
No. Set doesn't return anything so you cannot cast the result.

Or this (first = Get)?

B4X:
       Buttons.Get As B4XView(5).Text = Buttons.Get As B4XView(2).Text
Yes.
 

agraham

Expert
Licensed User
Longtime User
B4X:
Buttons.Get As B4XView(5).Text = Buttons.Get As B4XView(2).Text
This looks totally wrong to me with the indexer dissociated from its target. I still don't understand why not
B4X:
Buttons.Get(5) As B4XView.Text = Buttons.Get(2) As B4XView.Text
I guess when needed I will stick to the old ways. I don't like this at all 😟
 

Sandman

Expert
Licensed User
Longtime User
i mean you can, not use it...
Yes, thankfully the existing syntax will continue to exist, which I surely will use instead.

The problem is that we will never get to improve the new syntax after it's launched. ("Improve" is subjective from my perspective, of course - I realize you like it.)

Erel has made it very clear how important backward compatibility is for him, so what gets launched is what we'll live with for years to come at least. And as it happens, I wouldn't mind having access to inline casting also, I'm just trying to do my best to give constructive feedback on how confusing I see the syntax as it is, and how it could be instead.

So, in short, we have a very small window in time where we can try to give input and affect how things get implemented. It's important to use that window, because once it closes, it never opens again.
 

LucaMs

Expert
Licensed User
Longtime User
In order to save a few lines of code and some variables, are we willing to make the source less readable?

i mean you can, not use it...
This would be true if you only work on code created by you but sometimes this is not the case.
Furthermore...
A programming language is not a random bag of features
[Erel was referring to inheritance, here]

I do not like it; it is due to my old age, most likely.
 

Heuristx

Active Member
Licensed User
Longtime User
There are many possibilities and most of them are good. One thing is true, though. The idea is:
Value As Type
and the value here is List.Get(i)
So strictly speaking, the syntax List.Get(i) As B4XView.Text is the best match to that.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Thank you all for the feedback. I went back to the drawing board and the result is a better design. It is simpler and also more powerful as it is no longer limited to methods:
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")
 
Last edited:

AnandGupta

Expert
Licensed User
Longtime User
It is simpler and also more powerful as it is no longer limited to methods:
Yes I can also read it clearly now and understand. The result is more obvious now.
I hope most members will agree.
 

agraham

Expert
Licensed User
Longtime User
It is simpler and also more powerful as it is no longer limited to methods:
It is indeed and hopefully of more general use. Will it work for all types? For example can I use it for casting any object to its known type?
B4X:
Sender.As(Button).Text = "Fred"
Dim String as Text = Sender.As(Button).Text
 

Peter Simpson

Expert
Licensed User
Longtime User
Wow @Erel,
I've been quietly following along with this thread and Post #53 is an elegant solution and awesome work.

I've said plenty of times in the past that this forum and it's members are easily and simply the best on the internet, and this whole thread proves it. You had @agraham, @alwaysbusy et el giving you precious feedback on a new upcoming B4X feature, and with all that input you've improved upon your previous solution.

Please anyone who knows of another forum where users often gets input of new upcoming features and can actually effect changes in that new feature before its final release, then please point me to that forum. I've never seen that happen on any other forums as usually the developers just release what they think is correct and their users just have to live with it.

Congratulation to Anywhere Software and all the thread posters for improving on what was already a good new feature and turning it into a great new feature 👏👏👏
 

aeric

Expert
Licensed User
Longtime User
I've never seen that happen on any other forums as usually the developers just release what they think is correct and their users just have to live with it.
This reminds me of what I read a few days ago in another forum. There was a small war inside 😅
P.s I am wondering who is the OP
 
Top