ErrorLevel should not be treated as Boolean

Share your ideas as to how the documentation can be improved.
john_c
Posts: 493
Joined: 05 May 2017, 13:19

ErrorLevel should not be treated as Boolean

Post by john_c » 07 May 2020, 12:13

Some examples in documentation treats ErrorLevel as boolean. This is not correct.

For example, here: https://www.autohotkey.com/docs/commands/FileReadLine.htm

Code: Select all

if ErrorLevel
whereas it should be

Code: Select all

if ErrorLevel != 0

User avatar
boiler
Posts: 17242
Joined: 21 Dec 2014, 02:44

Re: ErrorLevel should not be treated as Boolean

Post by boiler » 07 May 2020, 13:36

It is correct to be treated as Boolean because it's either 0 (False) indicating no error or 1 or 2 (truthy, meaning evaluated as True in a Boolean context) indicating an error. I use if ErrorLevel (or usually if !ErrorLevel) almost every day. For example, I (and a lot of people) use if !ErrorLevel after ImageSearch to indicate an image was found.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: ErrorLevel should not be treated as Boolean

Post by swagfag » 07 May 2020, 14:08

if it quacks like a boolean... something something

john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: ErrorLevel should not be treated as Boolean

Post by john_c » 07 May 2020, 14:21

@boiler Yes, I understand that if I need to check whether ErrorLevel is OK or not, I can treat it as Boolean.

But actually, it can contain a lot of different values, they are not limited to 1 and 0. Hence, it doesn't look really correct to treat it in such a way.

Maybe I overthink it.
Last edited by john_c on 07 May 2020, 15:55, edited 1 time in total.

gregster
Posts: 9095
Joined: 30 Sep 2013, 06:48

Re: ErrorLevel should not be treated as Boolean

Post by gregster » 07 May 2020, 14:26

john_c wrote:
07 May 2020, 14:21
But actually, it can contain a lot of different values, they are not limited to 1 and 0. Hence, it doesn't look really correct to treat in such a way.
Well, actually that seems pretty consistent with the general handling of boolean values in AHK (and similar to a number of other programming languages, btw):
https://www.autohotkey.com/docs/Variables.htm#Expressions wrote:Boolean values: When an expression is required to evaluate to true or false (such as an IF-statement), a blank or zero result is considered false and all other results are considered true. For example, the statement if ItemCount would be false only if ItemCount is blank or 0. Similarly, the expression if not ItemCount would yield the opposite result.

Post Reply

Return to “Suggestions on Documentation Improvements”