"outer" keyword - get a reference to the outer class (parent, super, root, inner)

Post your working scripts, libraries and tools
User avatar
nnnik
Posts: 3553
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: "outer" keyword - get a reference to the outer class (parent, super, root, inner)

11 Apr 2018, 03:12

I see - this is the first time I have seen base.method() being used. I just always misunderstood this part of the documentation.
Recommends AHK Studio
iseahound
Posts: 355
Joined: 13 Aug 2016, 21:04
GitHub: iseahound

Re: "outer" keyword - get a reference to the outer class (parent, super, root, inner)

11 Apr 2018, 20:30

lexikos wrote:You can use this.base for that.

Actually using this.base gives me undesirable behavior...

Code: Select all

class a {
    save(){
        this.base.var := 7
    }
}

a.save()
MsgBox % a.var ; blank

class b extends Empty {
    save(){
        this.base.var := 7
    }
}

class Empty {
    ; Nothing to see here.
}

b.save()
MsgBox % b.var ; 7


; Expected, but undesirable behavior. 
MsgBox % empty.var
where if I have class a, and I want to hide a variable under its super global scope, it doesn't work unless a base class exists and extends creates that base class. Also for obvious reasons, I would not like to modify class Empty.
lexikos
Posts: 6207
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: "outer" keyword - get a reference to the outer class (parent, super, root, inner)

11 Apr 2018, 23:30

Just use 'this' in that case...
iseahound
Posts: 355
Joined: 13 Aug 2016, 21:04
GitHub: iseahound

Re: "outer" keyword - get a reference to the outer class (parent, super, root, inner)

20 Nov 2018, 21:40

Small update to the first post to address cases where the "outer" property is nested in one class. The user can now call this.outer["subclass"] to retrieve the global class named "subclass". This is useful when the class may or may not be nested inside another class.

Code: Select all

outer[p:=""] {
   get {
      ; Determine if there is a parent class. this.__class will retrive the
      ; current instance's class name. Split the class string at each period, 
      ; using array notation [] to dereference. Void if not nested in at least 2 classes.
      if ((_class := RegExReplace(this.__class, "^(.*)\..*$", "$1")) != this.__class)
         Loop, Parse, _class, .
            outer := (A_Index=1) ? %A_LoopField% : outer[A_LoopField]
      ; Test if this property is nested in one class. If so, return the global class "p".
      ; Otherwise if no subclass (p) is specified, return an empty string.
      if IsObject(outer)
         return (p) ? outer[p] : outer
      else
         return (p) ? %p% : ""
   }
}

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], leosouza85 and 39 guests