#Warn when assigning to class name Topic is solved

Propose new features and changes
Posts: 110
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/8795 ... -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

#Warn, All

x:=new test()
x.Hello("works (obviously)!")

some:=new something()
y:=new test()
y.hello("works, too!")


z:=new test()
z.hello("no more hellos from here.")

MsgBox, Done.

class something
		local test
		test:=new test()
		test.Hello("works seemingly fine for the first couple of thousand times, then crashes the script.")

class test
		msgbox, Hello %Txt%
Posts: 3522
Joined: 17 Jul 2016, 01:02

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

Error:  Missing class object for "new" operator.

	007: x.Hello("works (obviously)!")
	009: some := new something()
	010: some.TestClassUntouched()
	011: y := new test()
	012: y.hello("works, too!")
	014: some.TestClassRedefined()
	016: some.KillTestClassWithoutWarning()
--->	017: z := new test()
	018: z.hello("no more hellos from here.")
	020: MsgBox "Done."
	021: Return
	027: {
	029: test := 0
	030: }
	033: {

The current thread will exit.

just me
Posts: 5824
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.
Posts: 110
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.
Posts: 6207
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.
Posts: 110
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 7 guests