__New(...) and base constructor Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
justAnotherUser
Posts: 10
Joined: 28 May 2017, 07:03

__New(...) and base constructor

14 Nov 2017, 18:06

Hi, forum

In other laungages (C++, Java) the first instruction within a constructor is calling a superclass constructor; if not done explicitly, an implicit call to an empty constructor is provided. In AHK this doesn't seem to be the case. Following example

Code: Select all

mySubObj := new mySubclass()
fileAppend, % mySubObj.var " , " mySubObj.subVar "`n", *

class myClass {
  var := ""
  __New() {
    this.var := "Some default value"
  }
}

class mySubclass extends myClass  {
  subVar := ""
  __New() {
;   base.__New() 
    this.subVar := "Another default value"
  }
}  
The output would be
> , Another default value
the value of subVar; the var field remains empty, base.__New() is not implicitly called. If i would remove the commenting out in the above code, the output would be
> Some default value , Another default value

The doc states
Whenever a derived object is created with the new keyword ..., the __New method defined by its base object is called.
From my observation, it isn't, if not done manually. On the other hand, the base object is not empty, it is still created, even if base.__New() is not called. Quite confusing.
So ... well, what exactly is going on, when chaining constructors and creating derived objets with the new keyword ?
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: __New(...) and base constructor  Topic is solved

14 Nov 2017, 20:01

The base object in your case is mySubclass and __New is called here.
The docs also describe this situation:

Code: Select all

myObj := new myClass()
myObj.var:="Another value"
test:=new myObj
MsgBox % test.var

class myClass {
  var := ""
  __New() {
    this.var := "Some default value"
  }
}
In your example var is set in mySubObject because no instance of MyClass is created.
If base.__New() would be called automatically it could lead to unexpected results.
You should init all keys in base class to keep it clear, base.__New() in your example is the same as doing this.var := "Some default value" instead.

Code: Select all

mySubObj := new mySubclass()
MsgBox % mySubObj.var

class myClass {
  var := ""
  __New() {
    this.var := "Some default value"
  }
}

class mySubclass extends myClass  {
  subVar := ""
  __New() {
    this.Var := "Another default value"
    base.__New()
  }
}
justAnotherUser
Posts: 10
Joined: 28 May 2017, 07:03

Re: __New(...) and base constructor

15 Nov 2017, 14:22

I think i got it, thx for help, HotKeyIt
For some reason i thought %myObject%.base to be an equivalent to
Java's super, but it isn't. It's rather an equivalent to Java's
getClass() (well, sort of).
About initing keys -- of course i could assign default values directly
in the class definition. I did not for example purpose, to make the
constructors to actually do something.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Holarctic, jameswrightesq and 420 guests