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()
some.TestClassUntouched()
y:=new test()
y.hello("works, too!")
some.TestClassRedefined()
some.KillTestClassWithoutWarning()
z:=new test()
z.hello("no more hellos from here.")
MsgBox, Done.
return
class something
{
TestClassUntouched()
{
local test
test:=0
}
TestClassRedefined()
{
test:=new test()
test.Hello("works seemingly fine for the first couple of thousand times, then crashes the script.")
}
KillTestClassWithoutWarning()
{
test:=0
}
}
class test
{
Hello(Txt)
{
msgbox, Hello %Txt%
}
}