Type-checks for boolean built-in variables

Propose new features and changes
Descolada
Posts: 1202
Joined: 23 Dec 2021, 02:30

Type-checks for boolean built-in variables

Post by Descolada » 01 Jul 2023, 11:54

Hello,
I propose that AHK should throw type errors when boolean built-in variables are tried to be set to non-integer/non-boolean values.
Example where this would be useful:

Code: Select all

A_DetectHiddenWindows := "Off" ; Actually sets A_DetectHiddenWindows to 1
These kinds of variables are A_ListLines, A_DetectHiddenWindows, A_DetectHiddenText, A_StoreCapsLockMode, A_AllowMainWindow, A_IconHidden. Any corresponding functions such as DetectHiddenWindows() should act in the same kind.

lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: Type-checks for boolean built-in variables

Post by lexikos » 02 Jul 2023, 05:50

The documentation for A_DetectHiddenWindows defers to DetectHiddenWindows for details.

When a parameter type is "Boolean", this is a contract stating that an empty string or zero value will be considered false and any other value will be considered true. In other words, no value is "non-boolean". We cannot simply break this contract without also bumping the major version; i.e. v3.0.
When a value is required to be either true or false, a blank or zero value is considered false and all other values are considered true.
Source: Concepts and Conventions | AutoHotkey v2
For v2.1, I have some ideas for allowing portions of a script (i.e. modules) to opt in to changes that would otherwise be backward-incompatible.

If you wish to improve error-detection in the current version, you can wrap the various functions with your own custom functions (or some other interface), or redefine the "built-in" functions and use those.

Code: Select all

BoolInteger(Value) {
	if (Value != 0 && Value != 1)
		throw (Value is Number ? ValueError : TypeError)("Value must be 0 or 1", -2, Value)
	return Value
}
DetectHiddenWindows.DefineProp 'Call', {
	Call: (Function, Value) => 
		(Func.Prototype.Call)(Function, BoolInteger(Value))
}
MsgBox A_DetectHiddenWindows
DetectHiddenWindows 1
MsgBox A_DetectHiddenWindows
DetectHiddenWindows "off"
This can be optimized.

I would personally only choose to prohibit non-numeric values, due to the legacy of using "Off". However, neither A_DetectHiddenWindows := "Off" nor DetectHiddenWindows "Off" ever worked in v1, and DetectHiddenWindows Off would produce an error in v2 unless you assign a value to Off, in which case it could work.

Descolada
Posts: 1202
Joined: 23 Dec 2021, 02:30

Re: Type-checks for boolean built-in variables

Post by Descolada » 02 Jul 2023, 07:01

@lexikos, just to clarify, I'm not requesting to change AHKs definition of "boolean" at large, but only for those mentioned built-in variables and functions.

My main gripe with the current implementation is that coming from v1 I still find myself sometimes using the On/Off keywords, which in v2 lead to unexpected results (eg DetectHiddenWindows "Off"), and since a lot of forum posts and documentations are from v1, this mistake is easy to make. Newer users might also mistakenly try DetectHiddenWindows "False" which has the same problem.
To make matters worse, some functions *do* allow "On/Off" such as Critical.

One other possibility would be to change the behaviour of built-in variables like A_DetectHiddenWindows to work similarly to Critical, that is, instead of Boolean use OnOffNumeric. This would technically be a breaking change, but I'd recon the amount of users using "Off" in A_DetectHiddenWindows := "Off" as True is non-existant and the breaking change wouldn't affect anyone, so perhaps you'd consider it for v2.1?

Post Reply

Return to “Wish List”