[v2] Class method static variables

Share your ideas as to how the documentation can be improved.
Descolada
Posts: 1123
Joined: 23 Dec 2021, 02:30

[v2] Class method static variables

Post by Descolada » 27 Jan 2023, 14:27

Could it be added under the explanation for static variables that static variables inside class methods are shared between class instances?

Code: Select all

class Test {
    Store(val?) {
        static storedVal := ""
        if !IsSet(val)
            return storedVal
        storedVal := val
    }
}

o1 := Test()
o1.Store(5)
o2 := Test()
o2.Store(4)
OutputDebug(o1.Store()) ; 4
OutputDebug(o2.Store()) ; 4

SandyClams
Posts: 63
Joined: 02 Jul 2020, 11:55

Re: [v2] Class method static variables

Post by SandyClams » 27 Jan 2023, 15:17

if you know the things the documentation says about classes and static variables, I think you should already know that those variables would be shared between class instances. Namely, that methods defined in a class declaration are added to a Prototype object which is shared by all instances rather than duplicated for each instance. In other words, it would only make sense that the static variable is shared, since it's only a single variable defined inside a single method.

Descolada
Posts: 1123
Joined: 23 Dec 2021, 02:30

Re: [v2] Class method static variables

Post by Descolada » 28 Jan 2023, 05:35

@SandyClams, to make this implication, the user has to know (or educate themselves) that:
1) Class methods and properties are stored in a Prototype object
2) Non-static properties are not shared between class instances (that is, they are copied), yet non-static methods refer to the same underlying function
3) The class method static variable is not, for example, stored in the "this" variable as a special case, or there isn't some other workaround implemented

I'm not saying one can't imply it, rather it might save time and effort for people who are not intimately knowledgeable with classes.

iseahound
Posts: 1444
Joined: 13 Aug 2016, 21:04
Contact:

Re: [v2] Class method static variables

Post by iseahound » 28 Jan 2023, 22:06

This thread didn't make any sense to me. "static variables inside class methods" doesn't exist as a concept at all.

There is:
  • Objects hold references to functions
  • Functions may have static variables
The idea that a static variable would produce a different effect when its reference is held by an object... Just use properties with a simple getter and setter.

https://www.autohotkey.com/docs/v2/Objects.htm#Custom_Classes_property

Post Reply

Return to “Suggestions on Documentation Improvements”