Bug? Boolean = 1 or 0 compiles but then crashes

ac9ts

Active Member
Licensed User
Longtime User
I do a lot of embedded programming and use 1 for true and 0 for false. Out of habit, I used 1 for true in a B4A program. The program compiled and installed but then crashed when it was run. It took a while to figure it out since the SomethingSomething was a rare occurrence and I glossed over the HD=1 from familiarity.

Bug fix request: Flag as an error in the IDE or allow it in parseBoolean.

B4X:
Dim HD as Boolean

' This compiles
If SomethingSomething then HD=1

The resulting error log (trimmed)
B4X:
java.lang.RuntimeException: Cannot parse: 1 as boolean
    at anywheresoftware.b4a.BA.parseBoolean(BA.java:640)
    at anywheresoftware.b4a.BA.ObjectToBoolean(BA.java:710)
 
Last edited:

emexes

Expert
Licensed User
Longtime User
+1

and I'd go with a best-of-both-worlds option:

a IDE warning to nag the programmer to do it right, and
a default automatic cast from numeric to Boolean, same as happens with numeric to String.

Although that then brings up the question of: should there also be a default automatic case in the other direction, and is True positive 1 (C tradition) or negative 1 (BASIC tradition). I'd probably go with whatever VB does, to keep at least one group of people happy. On the other hand, B4X has already taken the Java path more often than not, so perhaps it's best to stick with that. Or toss a coin.
 

BlueVision

Well-Known Member
Licensed User
Longtime User
Hmmm... I don't like a change.

That's just the way it is. Every programming language has its own syntax.

Personally, I see no reason to have a Boolean variable interpreted by B4X in this way. Its value can have two states, and those are true and false. I think that this is actually the closest thing to the meaning of a Boolean variable.

You are free. You can for example, define the variable as an integer if you personally prefer 0 and 1.
The IDE also points out the problem correctly. You cannot assign a value of 0 or 1 to Boolean variable in B4X.

It is defined that way and should not be changed. In my opinion, it would make reading and debugging the source code more difficult. Regardless of personal preferences.
 

ac9ts

Active Member
Licensed User
Longtime User
Hmmm... I don't like a change.

That's just the way it is. Every programming language has its own syntax.

Personally, I see no reason to have a Boolean variable interpreted by B4X in this way. Its value can have two states, and those are true and false. I think that this is actually the closest thing to the meaning of a Boolean variable.

You are free. You can for example, define the variable as an integer if you personally prefer 0 and 1.
The IDE also points out the problem correctly. You cannot assign a value of 0 or 1 to Boolean variable in B4X.

It is defined that way and should not be changed. In my opinion, it would make reading and debugging the source code more difficult. Regardless of personal preferences.
The bug is, you CAN assign a value of 1 or 0 to a Boolean variable, the program will compile, then it will crash when run on the device. The IDE did NOT point out the issue in v13.10. The attached test project compiles and crashes.
 

Attachments

  • Test.zip
    8.9 KB · Views: 77

BlueVision

Well-Known Member
Licensed User
Longtime User
OK, got it now.

The variable declaration is correct, the value assignment is also syntactically correct. Unfortunately, the term assigned to the variable is of the wrong type. I think that such programming errors usually only become apparent during the program run. It is interesting that the compiler can also be tricked in this way.
It won't be easy to detect this programming error within the IDE. It happens quite often, for example, that a variable is read from an SQL cell that has not yet been filled, or that a loop is interrupted with an ‘out of bounds’ because not enough values are available (in a string, for example).
I can only think of a German programming saying, which you should not take personally under any circumstances.
The saying goes:

IDIOTS ARE INVENTIVE, YOU JUST CAN'T PREDICT EVERYTHING. ;)

At least the error message during the program execution then describes quite accurately what went wrong.
 

tonigau

Member
In B4R IDE it shows a squiggly red underline & a MouseOver helptext If I try setting a boolean var with 1. (I did not try to compile)

When I set or get the bit state of a numeric variable the real state of that memory bit is a 1 or 0,
A 1 or 0 to represent a boolean state is intuitive to me. The compiler could? just preprocess/interpret it as a boolean state.

Bool as 1 msg.png
.
 
Top