Difference between instance variables and "this." variables created inside __New()

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wpb
Posts: 150
Joined: 14 Dec 2015, 01:53

Difference between instance variables and "this." variables created inside __New()

15 Feb 2016, 06:54

Is there any difference between the following?

Code: Select all

Class MyClass {
	__New() {
		this.MyArray := Object()
		return this
	}
}

Code: Select all

Class MyClass {
	MyArray := Object()
}
Both are instance variables, only created when _New() is called, as far as I can see from the documentation. In the second example, MyArray doesn't actually exist until an object of MyClass class is created, correct?
User avatar
trismarck
Posts: 506
Joined: 30 Sep 2013, 01:48
Location: Poland

Re: Difference between instance variables and "this." variables created inside __New()

15 Feb 2016, 21:40

wpb wrote:Both are instance variables
The meaning of this sentence is: both code snippets create a key-value pair in the 'instance' object (update: I mean, they would create it if the class was instantiated). Only the second example contains the Instance Variable. The first example contains the __New method; that __New method assigns a key-value pair to the 'instance' object.
wpb wrote:only created when _New() is called
In both examples, the class object doesn't have the method key _New:

Code: Select all

for k,v in MyClass
	out .= k "`t" v "`n"
msgbox % out
wpb wrote:only created when _New() is called
If __New is called directly and a non-object value is passed as the first parameter, MyArray key won't be created:

Code: Select all

Class MyClass {
	__New()
	{
		this.MyArray := [2]
		msgbox % ObjHasKey(this,"MyArray") ; ""
		msgbox % isObject(this.MyArray)	; 0
	}
}

myClass.__New.Call(3)
wpb wrote:In the second example, MyArray doesn't actually exist until an object of MyClass class is created, correct?
Yes. This is also true for the first example. To clarify, MyClass class also lacks that key.
wpb wrote:Is there any difference between the following?
Yes.

The compiler puts Instance Variables from the body of the class into a hidden method __Init (puts them in the form of assignments (I guess) ) of the 'class' object. The __Init method also contains one other line of code that the compiler inserts into it, but the insertion only happens in certain circumstances (related to subclassing and calling __Init of all classes in the inheritance chain in the right order).

One difference is that it is possible to overwrite in __New what was assigned in __Init:

Code: Select all

Class MyClass {
	MyArray := [1]
	__New()
	{
		; msgbox % this.HasKey("MyArray")
		this.MyArray := [2]
	}
}

obj := new MyClass()
msgbox % obj.myArray[1]	; 2

The other related thing is what new operator does (vs what calling __New directly would do):
  • create an object
  • assign the base to the object
  • call __Init upon the base of the object, passing the object (vs upon object itself I guess)
  • call __New upon the object the base of the object (like for __Init).
Last edited by trismarck on 17 Feb 2016, 01:20, edited 1 time in total.
lexikos
Posts: 9599
Joined: 30 Sep 2013, 04:07
Contact:

Re: Difference between instance variables and "this." variables created inside __New()

15 Feb 2016, 22:47

All "instance variables" are initialized before __New is called. This is important if, for example, you define an instance variable in a base class and need to perform some other initialization in the derived class' __New method before calling base.__New().

While you currently cannot define both a static/class variable and a non-static/instance variable with the same name in the same class, you can define a static/class variable and then "obscure" it by creating a key-value pair in the instance. For example:

Code: Select all

class C {
	static V := "class"
	__New() {
		this.V := "instance"
	}
}
I := new C
MsgBox % C.V " | " I.V
trismarck wrote:call __Init upon the base of the object, passing the object (vs upon object itself I guess)
Why do you say that? The only observable difference would be that calling the new object would use any "__init" key defined by the new object itself, whereas calling the base would not; but since none of the instance variables have been initialized yet, there's no "__init" key and therefore no difference.
wpb
Posts: 150
Joined: 14 Dec 2015, 01:53

Re: Difference between instance variables and "this." variables created inside __New()

19 Feb 2016, 07:11

Thank you both for your insights. I appreciate you taking the time to clarify things.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Descolada and 332 guests