In v2-changes, I wrote:
#MustDeclare [On|Off]enables or disables must-declare mode. If used in a func lib file, it affects only that file and any files it explicitly #includes. However, it inherits its default setting from the main script. It is positional, but cannot be used inside a function (due to the following point). Functions default to either assume-local or must-declare depending on the setting at the point the function is defined, but this can be overridden by using
Staticwithout a variable name.
Var varnamedeclares a global variable without making it super-global (visible in all functions by default). These declarations are only necessary when #MustDeclare is enabled.
Chris wrote:A #MustDeclare directive has also been proposed, which is of special benefit for large scripts. Although this directive would overlap fairly heavily with #ShowWarnings, it might have enough new functionality to be worthwhile. But if implemented, some way of declaring global variables would need to be introduced, perhaps by allowing the global keyword to be used outside of functions. However, it might be best to reserve "global" for use in declaring "super globals", which are visible to all functions. So maybe the generic declaration keyword can be "declare" or "var".
Source: AutoHotkey v2
I consider it inconvenient and a poor fit for AutoHotkey.
I think that if the purpose of #MustDeclare is to make large scripts more maintainable, it would make sense to encourage the use of functions, where the
varkeyword isn't needed. Semi-global variables (variables which are accessible only outside of functions by default) make more sense in a "lazy" context, where they're created by using them without declaration.
The addition of
varjust for semi-globals seems like a "patch" to the language rather than a proper part of it. If later lexically scoped variables become possible,
varwould be superseded, and there'd have to be yet another var-declaring keyword (which seems like even more of a problem because of how generic "var" sounds).
Currently #MustDeclare causes AutoHotkey to stop at the first undeclared variable. A script editor could implement required declarations much more effectively, by highlighting potential errors as soon as they are written or appear on screen. The apparent problem with that approach is that without
var, there would be no way to declare semi-global variables; but it's only a problem if semi-globals belong in that sort of script in the first place.