Boolean? Or integer?
-
- Posts: 45
- Joined: Feb 07, 2016 18:13
Boolean? Or integer?
Hello,
I have the following line in my program:
loop until (in<>"") and (unique=true)
and this is causing a warning 38: "Mixing operand data types may have undefined results". Despite the warning, the program is working as expected, but I would prefer to make a clean program that gives no warnings.
My variable "in" is a string, while "unique" is a boolean, but I thought that the expressions on both sides of the "and" would evaluate to booleans!
Can anybody tell me where my mistake is?
Manfred
I have the following line in my program:
loop until (in<>"") and (unique=true)
and this is causing a warning 38: "Mixing operand data types may have undefined results". Despite the warning, the program is working as expected, but I would prefer to make a clean program that gives no warnings.
My variable "in" is a string, while "unique" is a boolean, but I thought that the expressions on both sides of the "and" would evaluate to booleans!
Can anybody tell me where my mistake is?
Manfred
Re: Boolean? Or integer?
You do not make a mistake there.
This is a bug not still corrected:
#797 relational ops can cause "mixing bool/non-bool operands" warnings
A workaround to suppress the warning:
loop until cbool(in<>"") and (unique=true)
This is a bug not still corrected:
See the bug report:dkl wrote:Also, the warnings shouldn't appear anymore now if mixing booleans with 0/-1 (e.g. true and (-1)), or integers with true/false (e.g. 123 and true). If I remember correctly, that's how it was intended, it was just not working. This gives better backwards compatibility with code which used the old-school integer TRUE/FALSE definitions.
#797 relational ops can cause "mixing bool/non-bool operands" warnings
A workaround to suppress the warning:
loop until cbool(in<>"") and (unique=true)
Re: Boolean? Or integer?
Yep, CBool() fixes the issue, testing code:Compiled with Error checking as follows:
fbc -s console -exx -w pedantic "Program_Name.bas" == NO Errors / Warnings.
Code: Select all
Dim As Double start = Timer
Dim As String in = ""
Dim As Boolean unique = FALSE
Do
If (Timer - start) > 3.0 Then unique = TRUE ' run for 3 Seconds
in += "." ' add a Dot every run
Print in ' show current string
Sleep 250, 1 ' give CPU a Break
Loop Until CBool(in <> "") AndAlso (unique=true)
' alternative: Loop Until unique AndAlso CBool(in <> "") ' a shorter way to check Boolean = TRUE
Print : Print "any key ends ... "
Sleep : End
fbc -s console -exx -w pedantic "Program_Name.bas" == NO Errors / Warnings.
-
- Posts: 45
- Joined: Feb 07, 2016 18:13
Re: Boolean? Or integer?
Ah, thanks!
Yes, with that change the warning doesn't show up.
When bug 797 gets fixed, I will try to remember to remove those cbool functions! Just to save some bytes and microseconds...
Manfred
Yes, with that change the warning doesn't show up.
When bug 797 gets fixed, I will try to remember to remove those cbool functions! Just to save some bytes and microseconds...
Manfred
Re: Boolean? Or integer?
Why not use conditional compilation with an identifier corresponding to each bug not still solved?
For example using:
- #Define bugxyz
at the program top for each bug xyz.
- Once that a bug is solved in the compiler version, it is sufficient to suppress (put in comment) only the line defining the bug at the top of the program.
Example with MrSwiss's code:
For example using:
- #Define bugxyz
at the program top for each bug xyz.
- Once that a bug is solved in the compiler version, it is sufficient to suppress (put in comment) only the line defining the bug at the top of the program.
Example with MrSwiss's code:
Code: Select all
#Define bug797
Dim As Double start = Timer
Dim As String in = ""
Dim As Boolean unique = FALSE
Do
If (Timer - start) > 3.0 Then unique = TRUE ' run for 3 Seconds
in += "." ' add a Dot every run
Print in ' show current string
Sleep 250, 1 ' give CPU a Break
#Ifdef bug797
Loop Until CBool(in <> "") AndAlso (unique=true)
#Else
Loop Until (in <> "") AndAlso (unique=true)
#Endif
' alternative: Loop Until unique AndAlso CBool(in <> "") ' a shorter way to check Boolean = TRUE
Print : Print "any key ends ... "
Sleep : End
-
- Site Admin
- Posts: 6323
- Joined: Jul 05, 2005 17:32
- Location: Manchester, Lancs
Re: Boolean? Or integer?
I have to be honest: when booleans were introduced, I expected them to be used wherever it made sense, i.e. for operations returning true/false values (comparisons, AndAlso/OrElse, and functions like EOF).
If that ever becomes the case, it wouldn't make any difference in the emitted code whether a CBool were there or not.
If that ever becomes the case, it wouldn't make any difference in the emitted code whether a CBool were there or not.
Re: Boolean? Or integer?
Now that the bug report #797 relational ops can cause "mixing bool/non-bool operands" warnings is solved by the commit Hide "mixed operands" warning if the nonbool is a relational BOP, the topic title could be tagged as "solved by fbc 1.06.0".
Re: Boolean? Or integer?
I think that if milliseconds are to be saved then the order should be swapped then andalso used instead of and.
The string compare in= "" is kinda sluggish.
This is Win 32 anyhows.
The string compare in= "" is kinda sluggish.
This is Win 32 anyhows.
Code: Select all
dim as boolean unique
dim as string in
dim as double t=timer
dim as integer counter
do
counter+=1
if counter>100000000 then in=" ":unique=true
loop until cbool(unique=true) andalso (in<>"")
print timer-t
sleep
Re: Boolean? Or integer?
To all reading this:
Do you prefer to write
since it implies that unique is a boolean variable?
Or rather the following which is shorter (and requires no cbool()):
I would probably rename unique to isUnique:
Then the "is" part gives away that it is a boolean variable.
Do you prefer to write
Code: Select all
loop until cbool(unique = true) andalso (in <> "")
Or rather the following which is shorter (and requires no cbool()):
Code: Select all
loop until unique andAlso (in <> "")
Code: Select all
loop until isUnique andAlso (in <> "")
Re: Boolean? Or integer?
Code: Select all
loop until isUnique andalso len(in)>0