Overwritte Class Variables | ExitApp, __Delete & Static Variables

Discuss the future of the AutoHotkey language
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Overwritte Class Variables | ExitApp, __Delete & Static Variables

23 May 2017, 22:06

Hi,
Maybe I've lost something important, my head hurts (I need to sleep :crazy: ), but I think this behavior should be changed.

Code: Select all

A := New Size() ;OK		:)
A := 0
MsgBox("A")

F()

B := New Size() ;ERROR!    :'(
B := 0
MsgBox("B")

F()
{
	Size := 0
}

Class Size
{
}
What is the reason why this is allowed?.
I think, that if a class variable is overwritten into a function, when function return should be restored...
guest3456
Posts: 3453
Joined: 09 Oct 2013, 10:31

Re: Overwritte Classes -.-

23 May 2017, 23:01

Flipeador wrote: What is the reason why this is allowed?.
read:
https://autohotkey.com/docs/Functions.htm#SuperGlobal
https://autohotkey.com/board/topic/1146 ... /?p=666930

the reason to make classes super global is so you dont have to do this:

Code: Select all

F() {
   global Size        ;// this not necessary 
   C := new Size()
}

but because of super globals, you do need to do this:

Code: Select all

F() {
   global
   local Size
   Size := 0
}
i guess you could omit the 'global' line above, because specifically declaring a local variable automaticlly does assume-global mode, however i find that not intuitive at all, so i would put the global declaration for clarity

User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Overwritte Classes -.-

23 May 2017, 23:23

guest3456 wrote:the reason to make classes super global is so you dont have to do this: [...code...]
Well, I never said that this is wrong, I think it's fine.
What I said was:
Flipeador wrote:if a class variable is overwritten into a function, when function return should be restored
By changing what I say, you can still do this without problems:

Code: Select all

F() {
   C := new Size()
}
But at the time of overwriting the variable, it must be restored.

Edit* Ok, You can use Local Size, But... Would not this have to be automatic? Who would want to overwrite a class? It doesn't make sense.
lexikos
Posts: 9494
Joined: 30 Sep 2013, 04:07
Contact:

Re: Overwritte Classes -.-

23 May 2017, 23:36

Flipeador wrote:I think, that if a class variable is overwritten into a function, when function return should be restored...
No, it shouldn't.

You seem to be describing local variables. It isn't a local variable. If you want a local variable, declare a local variable.

A class variable is an ordinary super-global variable. The only difference is that it is given a value at load-time, before the script executes.

I have considered adding a load-time warning for code which assigns to a class variable, but even with that, your code will not work. You must declare the variable or use a different name. You can't both be able to access the class and use the same name for a local variable.
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Overwritte Classes -.-

23 May 2017, 23:53

I understand, even so, overwriting a class does not make sense. :problem:
lexikos wrote:You seem to be describing local variables. It isn't a local variable. If you want a local variable, declare a local variable.
At the time of creating the post, I have not taken into account Local Size. Thank you, both of you.
lexikos wrote:I have considered adding a load-time warning for code which assigns to a class variable
It would be great. ;)
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Overwritte Classes -.-

24 May 2017, 00:31

I am overwriting classes at some occasions.
Recommends AHK Studio
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Overwritte Classes -.-

24 May 2017, 05:49

nnnik wrote:I am overwriting classes at some occasions.
A example would be interesting. :shock:
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Overwritte Classes -.-

24 May 2017, 16:16

I have another question, I do not know if this deserves a new post, but, anyway:
When the script terminates, why does this give an error?:

Code: Select all

V := New A
ExitApp

Class A
{
	Static Z := "*"
	__Delete()
	{
		MsgBox(A.Z)
	}
}
Would not it be better to first call __Delete(), then free the static variables?
Is this clarified somewhere in the documentation?
You can do this:

Code: Select all

Global V
OnExit("F")
V := New A
ExitApp

F()
{
	V := 0
}

Class A
{
	Static Z := "*"
	__Delete()
	{
		MsgBox(A.Z)
	}
}
But... Is there any particular reason?.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Overwritte Class Variables | ExitApp, __Delete & Static Variables

24 May 2017, 16:49

The error is not due to Z being freed, it is due to trying to invoke an object which doesn't exist, that is A. __Delete() is called when V is deleted, not A.
Objects wrote: __Delete is not called for any object which has the key "__Class". Class objects have this key by default.
Flipeador wrote:You can do this:
Also, you can do this :)
Spoiler
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Overwritte Class Variables | ExitApp, __Delete & Static Variables

24 May 2017, 18:06

Helgef wrote:Also, you can do this :)
:lol: I see. Thank you Helgef.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Overwritte Class Variables | ExitApp, __Delete & Static Variables

25 May 2017, 11:08

Helgef almost wrote: I don't know in which order AHK deletes stuff on shutdown.
Maybe in some alphabetical order :lol:

Code: Select all

V := New Z
ExitApp

Class Z
{
	Static A := "*"
	__Delete()
	{
		MsgBox(Z.A)
	}
}
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Overwritte Class Variables | ExitApp, __Delete & Static Variables

07 Apr 2018, 18:48

I would like to take up this topic again.
The current request: By default, show an error when trying to overwrite a class.
Edit* I see that this has already been implemented, sorry :roll: . However, I consider that ClassOverwrite should be ON by default (I also consider that this should be treated as an ERROR! and should not be allowed).

Regarding:
nnnik wrote:I am overwriting classes at some occasions.
I would like to see a good reason to do this. I see it as a bad practice. As a solution, an option in #Warn could be added to disable this check.
What is the point of declaring a class if you are going to overwrite it? :facepalm:
lexikos
Posts: 9494
Joined: 30 Sep 2013, 04:07
Contact:

Re: Overwritte Class Variables | ExitApp, __Delete & Static Variables

07 Apr 2018, 20:39

Obviously one would not define the class with the intention of not using it, but you can still use it before or after reassigning the variable.

For example:

Code: Select all

dostuff()

dostuff() {
	static x := Msg(config.file)
}

class config extends config._base {
	file := "config.ini"
	;...
	class _base {
		__get(p*) {
            this.base := ""
			config := new config
            return config[p*]
		}
	}
}

Msg(m) {
	MsgBox % m
}
Based on constant values in static/class variables.

I might consider making class names read-only identifiers (not variable) in v2. It would not reduce flexibility much since one can still clone the class and modify the original object as needed. However, the current framework has no provision for constants, and #Warn already gets the job done. Also, consider the fact that nested classes are (in current versions of the language) simply assigned to array elements, and a constant array element doesn't make much sense.

(#Warn ClassOverwrite is implemented by checking the code for assignments to variables which already contain an object before the script executes.)
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Overwritte Class Variables | ExitApp, __Delete & Static Variables

08 Apr 2018, 06:23

Well, in the example you have put, I really do not see a real and justified use. You are basically doing ClassName := new ClassName.
I think I can continue living with #Warn ClassOverwrite off by default :)
Thanks for the information. :wave:
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Overwritte Class Variables | ExitApp, __Delete & Static Variables

08 Apr 2018, 09:29

That is a Singleton pattern - it is one of the most commonly used patterns and has a lot of importance despite being such a minor piece of code.
You can still activate #Warn ClassOverwrite if you use the Singleton Pattern I described in the corresponding topic in the Tutorial or Tips and Tricks section.
Recommends AHK Studio
User avatar
Flipeador
Posts: 1204
Joined: 15 Nov 2014, 21:31
Location: Argentina
Contact:

Re: Overwritte Class Variables | ExitApp, __Delete & Static Variables

08 Apr 2018, 11:49

Thanks nnnik. There seems to be an interesting discussion about this. It seems to have some cons. In my case, I do not think I'll use it.
:wave:
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Overwritte Class Variables | ExitApp, __Delete & Static Variables

08 Apr 2018, 13:35

Well everything has it's pros and cons. However none of the cons mentioned have any weight under the fact that AHK is not made for unit tests or even has modules or interfaces.
All they are doing is nagging on a high level that doesn't even affect us AHK users.
( If you search for it hard enough you will find cons for everything )

Just for reference these people will laugh at you and think of you as a joke if you'd use functions.

It might be bad code practice but I'm a single developer working on my own code. In these cases bad code practices at this level dont matter.
At no point in time will my applications ever get so large that changing a singleton pattern to the cleaner alternative costs so much that not using them outweighs their pros.
You can use Singletons to define a single instance rather than a class. What you use that for is up to you.

I only recently started reading the GoF book - I still dont know all the patterns.
Recommends AHK Studio
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Overwritte Class Variables | ExitApp, __Delete & Static Variables

14 May 2019, 04:17

i have to second @Flipeador's proposal
invert classoverwrite, have it on by default, the directive turns it off. or introduce some specifier to render classes overwritable on a per class basis. overwriting classes is obscure and "fails"(?) silently, u won't know what's going on unless uve already read the docs about it.

see here for proof https://www.autohotkey.com/boards/viewtopic.php?f=76&t=64492
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Overwritte Class Variables | ExitApp, __Delete & Static Variables

14 May 2019, 05:34

Overwriting any variable in global scope can fail silently and have fatal consequences.
The issue is using global scope and not keeping track of it - #WarnClassOverwrite is just the tip of the iceberg.
Recommends AHK Studio
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Overwritte Class Variables | ExitApp, __Delete & Static Variables

14 May 2019, 19:46

which is exactly why it should be made more difficult to unknowingly overwrite classes

arguably variables, too, but currently immutability specifiers are a long way away probably

classoverwrite otoh can be switched around immediately

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: No registered users and 27 guests