#Warn when assigning to class name Topic is solved

Propose new features and changes
Elgin
Posts: 102
Joined: 30 Sep 2013, 09:19

#Warn when assigning to class name

19 Aug 2017, 06:25

I just had a very painful bug hunt for seemingly random crashes and memory leaks. The problem was that some local variables had the same names as class definitions in the script; it made sense at the time of writing the code to name then like that and the code ran fine for 10-15 minutes each time, so there didn't seem to be anything too obviously wrong with it. But assigning to these variables would change the class definition and break the code afterwards in seemingly completely unrelated places. I didn't expect it to be possible to simply overwrite class definitions to be honest :oops:. See the code below for an example.

While I now after some thinking and reading (https://autohotkey.com/board/topic/87951-argh-reason-why-variables-need-a-const-flag-or-a-non-hideable-flag-or-maybe-a-namespace-mechanism-or-perhaps-some-combination-of-the-3/) understand the rationale behind allowing to overwrite the super global class definitions and marvel at the theoretical possibilities of it, I somehow still doubt, it's what the average programmer has in mind when typing some local variable name into a function. This behavior is also bound to cause hard to find problems eventually when a class name is used by random chance as a variable in somebody else's library code. Most code I see around does not declare every local variable as explicitly local, which would be the only way to be safe from this.

So my suggestion: Would it be possible to add a warning to #Warn when a class name is being assigned to like a normal variable to make this kind of thing easier to spot?

This would in my opinion fit with #Warn in the spirit of the LocalSameAsGlobal warning to point out code that is probably a mistake.

Furthermore it would be nice if the possibility of redefining any class definition at run time would be documented more clearly in the manual. Unless I completely overlooked it, this is only documented for the default base object, which is a rather specific case.

Thanks for considering this.


Here's some code to illustrate the behaviors:

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

Helgef
Posts: 3096
Joined: 17 Jul 2016, 01:02
Contact:

Re: #Warn when assigning to class name

19 Aug 2017, 10:50

Hello Elgin, sorry to hear about your painful bugs, I know the feeling :(
Your wish and arguments are reasonable, I also think it would be nice if we could assume local for functions, or even for a whole class body.

I would strongly recommend you (and all) to use v2 instead, you would have got an error in your test script,

Code: [Select all] [Expand] [Download] (Untitled.txt)GeSHi © Codebox Plus



Cheers.
just me
Posts: 5431
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: #Warn when assigning to class name

19 Aug 2017, 11:04

See Functions / class methods - force-local mode.

I still think it's only half of the job to implement super-global variables without 'super-local' functions/methods.
Elgin
Posts: 102
Joined: 30 Sep 2013, 09:19

Re: #Warn when assigning to class name

19 Aug 2017, 11:32

Hi Helgef,

nice to know about the error in v2. That at least points in the right direction, even if it does not point directly to the culprit as a warning would do.
lexikos
Posts: 6045
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: #Warn when assigning to class name  Topic is solved

27 Dec 2017, 18:53

Added in v1.1.27.00: #Warn ClassOverwrite.

Off-topic: Added in v1.1.27.00: force-local mode.
Elgin
Posts: 102
Joined: 30 Sep 2013, 09:19

Re: #Warn when assigning to class name

05 Jan 2018, 11:17

Solved. Thank you very much!

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 2 guests