[a118] __Set examples and recursion

Get help for the alpha version of AutoHotkey v2 here. Please state the v2 version you are working with in the title when making a new topic.
SAbboushi
Posts: 233
Joined: 08 Dec 2014, 22:13

[a118] __Set examples and recursion

23 Jul 2020, 13:02

I've reviewed @Helgef's examples of how to avoid infinite recursion in property getters/setters by using a backing field.

But I can't for the life of me (2 days later) figure out how to avoid infinite recusion in Meta-Functions...

Code: Select all

#SingleInstance 
global xxx:=0

class c1 {
    __Set( name, args, value ){
        sleep 1
        ;this._%name% := value      ; infinite recursion (each iteration prefixes name param 
                                                 ;with another "_" i.e. "_a", "__a" ...
        ;super._%name% := value     ; Why doesn't this create property c1._a?
        this.base._%name% := value, xxx++  ; line 9
            /*
                First pass re: code execution in line 9:
                    this = x1 (c1 class instance)
                    name = "a"
                    I assume this.base doesn't result in call to __Set (passing name = base) 
                    because base already exists?
                    this.base._%name% results in a second call to __Set:
                        Call Stack now has two calls to __Set
                        this.base (x1.base i.e. c1.Prototype) passed to __Set
                        name = "_a" passed to __Set
                Second pass re: code execution in line 9:
                    this = x1.base (i.e. c1.Prototype)
                    name = "_a"
                    Sets this.base.__a = 12 (i.e. x1.base.base.__a i.e. c1.Prototype.base.__a)
                    Second call to __Set returns i.e. now there is only the first call to __Set in 
                    Call Stack
                        i.e. return from the second call to __Set to finish the first call to __Set
                    
                Third pass re: code execution in line 9:
                    this = x1 (c1 class instance)
                    name = "a"
                    Why isn't this.base._a (i.e. x1.base._a i.e. c1.Prototype._a) set to 12?
                    Finishes the first call to __Set re: x1.a
                    
                Also don't understand why this.base._%name% doesn't result in infinite 
                recursion?
            */
    }
}

x1:=c1.new()
x1.a:=12
So in addition to questions embedded in code:
How can I use __Set to assign the equivalent of this._%name% := value (the line I've commented out in code)?
lexikos
Posts: 7194
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: [a118] __Set examples and recursion

23 Jul 2020, 22:17

1. Create the backing field using a method that does not invoke __set, such as DefineProp, not by setting.
or
2. Check name in __set before doing anything.

Return to “AutoHotkey v2 Help”

Who is online

Users browsing this forum: No registered users and 4 guests