Wish Detecting the + sign as attempting to concatenate strings in the B4A compiler

Randy Younger

Member
Licensed User
I'm not sure how big of a deal this is to the community at large, but when I am translating code from a language like Swift that allows the + sign to be used to concatenate strings, the B4A compiler allows it, but then it blows up in runtime since the & character should be used. I tend to use a lot of string concatenation and so this is a big time waster for me that could be easily caught by the compiler (I think). Not a gotta have, but it would be nice...
 

DonManfred

Expert
Licensed User
Longtime User
B4X uses & to concatenate. + is for maths only.
 

JordiCP

Expert
Licensed User
Longtime User
It is not detected at compile time, because there are some cases, when the strings contain numeric expressions, annd they are casted as such.

B4X:
    Dim d As Int = "123" + "321"           ' Here the strings are casted to numbers, added, and assigned to d
    Log(d)    '<--- 444
    Dim d2 As String= "123" + "321"     ' Here the strings are casted as numbers, added, and then again casted as a string to d2
    Log(d2)    '<--- 444
    Dim d3 As Int = "A23" + "321"        ' Runtime booom!

Perhaps a warning would make sense.
 

Randy Younger

Member
Licensed User
Thank you for your replies. In Swift, "123" + "321" = "123321". I think most languages don't assume a + between literals means to cast those literals as integers. I get the convenience of this, and use it all of the time. Nevertheless, I am speaking of the specific case where I am translating large sections of code from Swift to B4A, and get hammered by the + sign as a concatenator a lot.
 
Top