#MustDeclare

Discuss the future of the AutoHotkey language

Do you use #MustDeclare?

I use #MustDeclare and var varname
2
7%
I use #MustDeclare but not var varname
0
No votes
I don't use #MustDeclare, but I might
14
47%
I won't use #MustDeclare
14
47%
 
Total votes: 30
lexikos
Posts: 5895
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

#MustDeclare

28 Mar 2015, 19:51

I am curious about what users think of #MustDeclare and how many use it.
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 Global, Local or Static without a variable name.

Var varname declares a global variable without making it super-global (visible in all functions by default). These declarations are only necessary when #MustDeclare is enabled.

For reference,
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 var keyword 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 var just for semi-globals seems like a "patch" to the language rather than a proper part of it. If later lexically scoped variables become possible, var would 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.
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: #MustDeclare

29 Mar 2015, 00:35

I don't really use both and I'm not sure if I'll use them - probably won't. Will removing these reduce a considerable amount of code size?
just me
Posts: 5329
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: #MustDeclare

29 Mar 2015, 05:45

My reason to suggest something like #MustDeclare was rather targeted on "#MustInitialize" before first use. So a simple MyVar := 0 would satisfy the requirements.
User avatar
jNizM
Posts: 2317
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: #MustDeclare

29 Mar 2015, 08:56

I just use #Warn in my scripts
[AHK] 1.1.28.02 x64 Unicode | [WIN] 10 Pro (Version 1803) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
joedf
Posts: 6240
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: #MustDeclare

05 Apr 2015, 08:28

Voted for:
I don't use #MustDeclare, but I might


What is the true advantage of this over the current features of #Warn, like jNizM mentioned?
Still, seems like an interesting idea.
Elgin
Posts: 101
Joined: 30 Sep 2013, 09:19

Re: #MustDeclare

07 Apr 2015, 07:06

I voted: Won't use

The only types of languages where I found the need to declare a variable helpful so far (even with large programs) were those which:
1. have strict variable types
2. necessarily have all variable declarations for their respective scope together in one block
This allows to quickly look up what everything is, which is nice.

When you can declare a variable anywhere in the code, it's already more of a nuisance than a help as you often have to resort to using Find to look stuff up and it does not add clarity.
When there's no strict variable types as in AHK, having to declare a variable merely adds a useless line to the code imho especially as it does not ensure that the variable has been properly initialised.

#Warn on the other hand does the job nicely for me.
lexikos
Posts: 5895
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: #MustDeclare

26 May 2017, 21:46

Bump. I'm interested in getting more data (votes) on this topic.
User avatar
jeeswg
Posts: 4274
Joined: 19 Dec 2016, 01:58
Location: UK

Re: #MustDeclare

27 May 2017, 08:35

Is this saying that all variables in the main body of a script (i.e. outside a function) would need to be declared as global/super-global. Just like #Warn forces all variables inside a function to be declared as local/global?

AFAICT AHK makes the reasonable assumption that variables in the script's main body are global, and variables inside a function are local, I would like an example where you would want to override this behaviour or where #Warn is useful. Are there *any* advantages to #Warn, because otherwise perhaps its usage should be actively discouraged because it just makes a script harder to maintain, and adds in redundant lines of code.

A related concern appears to be the apparently wildly unpopular 'No object to invoke' error message:
Error: No object to invoke - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=37&t=3818

Anyhow, I am not against any of these changes per se, even though I probably wouldn't use them, I would have a #DebugMode (or redefine #Warn) that would allow the scriptwriter to specify in a granular way, what error messages to show.
E.g. #Warn UseUnsetLocal UseUnsetGlobal LocalSameAsGlobal MustDeclare NoObjectToInvoke. Note: 'NoObjectToInvoke' could have a different name.

[EDIT:] To satisfy #Warn I would (will) have to amend around a thousand a functions I've written.

[EDIT:] If anything, we should be moving in the opposite direction e.g. have by default, or have an optional mode where obj.a := "hello", would create object 'obj' as well as key 'a', if 'obj' didn't already exist. Although this is not a personal priority of mine, and I'd be happy for things to remain as they are. It's reminiscent of FileCreateDir/DirCreate being needed before FileMove or FileCopy can occur, a somewhat silly inconvenience prompting me to rewrite those functions.

[EDIT:] Also to move in the opposite direction, it would be good to give advice to the user as to when objects have a negligible footprint on the script and so don't really need freeing, and leave the script to free them on script exit as it does with variables.

[EDIT:] If we're not careful we'll end up back at C++ again.
AutoHotkey is a free, open-source scripting language for Windows that allows users to easily create small to complex scripts for all kinds of tasks such as: form fillers, auto-clicking, macros, etc.
Last edited by jeeswg on 27 May 2017, 19:42, edited 1 time in total.
User avatar
joedf
Posts: 6240
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: #MustDeclare

27 May 2017, 11:02

I have no objection if it's off by default making it non-breaking for older scripts.
lexikos
Posts: 5895
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: #MustDeclare

28 May 2017, 01:21

joedf: Do I take that as "I won't use #MustDeclare"?

Also, I fail to see why it should matter whether older scripts are broken. I will be surprised if there are any scripts which will work as-is in the next v2 alpha.

Also, it should be obvious, given what I said in the first post, that I won't be making #MustDeclare the default.

;)
User avatar
joedf
Posts: 6240
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: #MustDeclare

28 May 2017, 09:31

Whoops! My bad! Forgot this was in v2.
Then it's a definite yes for me :+1:
Helgef
Posts: 2897
Joined: 17 Jul 2016, 01:02
Contact:

Re: #MustDeclare

29 May 2017, 09:30

If empty variables in math operators will throw exceptions, I do not see much value of this directive. Not declaring variables is very convenient, and adds to one of ahk's strengths, fast implementaion. Typical intended use of undeclared variables,

Code: [Select all] [Download] GeSHi © Codebox Plus

; Convenient
if !var ; var undeclared
var:="a"
str.=var ; str undeclared

Typical unintended,

Code: [Select all] [Download] GeSHi © Codebox Plus

; Catastrophic
y:=x+1 ; x undeclared, i.e., empty. If x where to be treated as 0, that would be even worse that empty string.

jeeswg wrote:A related concern appears to be the apparently wildly unpopular 'No object to invoke' error message:
Error: No object to invoke - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=37&t=3818

I wouldn't deduce that anything is wildly unpopular from that thread. No object error is very good, and makes the #MustDeclare directive even more superfluous.
If we are to increase the declaring of variables, it has to be for performance reasons, such as declaring a varible as an integer. This is probably outside the scope of this discussion though.

In summary if empty variables in math expressions throws exceptions, I will not use this, else, I might.

Cheers.
lexikos
Posts: 5895
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: #MustDeclare

29 May 2017, 20:54

Helgef, that won't protect you against misspelling of variable names in assignments, or concatenation, or other situations where no exception would be thrown (but #Warn warns you of some of them).
Helgef
Posts: 2897
Joined: 17 Jul 2016, 01:02
Contact:

Re: #MustDeclare

31 May 2017, 08:54

If #MustDeclare hadn't stopped the script at the first undeclared variable, but instead continued to the next or showed a list of all such variables, it would be much more useful for catching misspells, because I wouldn't need to declare all variables which I found convenient not to declare. Personally, I have very few problems with misspells because I use auto-complete any word in the file in notepad++. My misspelled variables are often consistently misspelled throughout the script :lol:
Helgef
Posts: 2897
Joined: 17 Jul 2016, 01:02
Contact:

Re: #MustDeclare

28 Dec 2017, 06:40

Hello again. I have reconsidered this topic, I want to use this for function libraries and such, but probably never for (non-super) global variables, that is, I do not want to do var var declarations but I want to delcare in functions, and be notified on mistakes. However, there seems to be a problem with built-in variables. Example,

Code: [Select all] [Download] GeSHi © Codebox Plus

#mustdeclare on
msgbox true

Anyways, I think the directive can be useful, and I go with, I use #MustDeclare but not var varname, if the built-in issue is resolved.

lexikos wrote:I am curious about what users think of #MustDeclare and how many use it.

I guess the answer is that no one is using it.

Cheers.
User avatar
nnnik
Posts: 2789
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: #MustDeclare

28 Dec 2017, 06:48

I might start using it
But I might miss a lot of typos since I work with Objects all the time.
Recommends AHK Studio
User avatar
jeeswg
Posts: 4274
Joined: 19 Dec 2016, 01:58
Location: UK

Re: #MustDeclare

28 Dec 2017, 08:27

- I'm glad someone reopened this thread. I've been thinking a lot about it since I read this thread.

- My concerns/priorities in this area would be:
- Must define function variables as global/local. Particularly for use with older scripts where force-local is not available.
- A way to list each function and all of its variables (not live, but before running). Perhaps a function to extract a list of variables from the selected text.
- Tidying. Notify of variables that don't have a consistent case.
- Listing all errors v. stopping at the first error. [EDIT: Or repeated MsgBoxes.]
- There are two ways to handle COM objects: [] (), this is fine, but a mode that forces one approach could be helpful for code consistency. [EDIT: Or better a way to list discrepancies, instead of error messages.]
- Having ways to do both #MustInitialize and #MustDeclare, as described by just me, would have benefits.

- I had a function fail because a variable was misspelled, (for most variables I use auto-replace hotstrings,) which shows a crucial benefit of turning on #MustDeclare for all variables temporarily.

- I don't know the full details or costs of any of this functionality, but I thought it worth mentioning.
- I like the use of the 'var' keyword. I don't honestly understand: 'and there'd have to be yet another var-declaring keyword', so would welcome any interested parties saying more about it.
- The way I would use things (probably), would be to turn the modes on temporarily to debug a script, then turn them off.
- Also, imagine converting a complicated AHK v1 script by someone else, to AHK v2. There is a hierarchy of errors, some errors are more critical than others. And more granular control allows you to fix the biggest roadblocks first. [EDIT: Sometimes when obscure errors are reported first, it becomes impossible to fix any errors.]
- Generally, the more filters that check your code, the better, e.g. 'no object to invoke' is very versatile, but it is also useful to be able to turn off virtually all the filters, as you convert code or write experimental code.
Last edited by jeeswg on 08 Jan 2018, 20:07, edited 1 time in total.
_3D_
Posts: 176
Joined: 29 Jan 2014, 14:40

Re: #MustDeclare

02 Jan 2018, 05:14

Won`t use.
#MustDeclare will prevent objects to obtain and to lose internal variables.

Code: [Select all] [Download] GeSHi © Codebox Plus

//C++ example
myfunction() { //wrong using of declared variables
int result; //declared but not init
return result; //using without init (probably result will be 0)
}

Code: [Select all] [Download] GeSHi © Codebox Plus

;AHKv2.0 example
myfunction() { ;no "wrong"
return result ;using without init (strongly result will be "") no declaration needed
}

Code: [Select all] [Download] GeSHi © Codebox Plus

class myClass {
}
myClass.undeclared:= 10 ;work
a:= new myClass() ;this.undeclared exist
a.undecl:= 100 ;this.undecl exist too
The only problem is when mess up the name.
AHKv2.0 use the future now.
lexikos
Posts: 5895
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: #MustDeclare

05 Jan 2018, 22:19

_3D_ wrote:#MustDeclare will prevent objects to obtain and to lose internal variables.
No, it won't. It will do what it does now, if it is not removed. #MustDeclare only affects local and global variables.

Edit: #MustDeclare was removed in commit 98766b027 (v2.0-a084).
Last edited by lexikos on 07 Apr 2018, 22:26, edited 1 time in total.
Reason: update

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 2 guests