Classes...

Propose new features and changes
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Classes...

12 Oct 2013, 09:17

This should return an error to make the user aware that it is an Error:

Code: Select all

tes:=new test()
Msgbox % tes.abc

class test{

__new()
{
this.abc:=1
this:=new test2()
this.abc:=0
}


}

class test2{
abc :=0
}
Recommends AHK Studio
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: Classes...

12 Oct 2013, 10:04

I don't think that it's an error, although I agree that it should not be advised(code design-wise).
If I understood correctly, "this" is just a variable containing a reference to an object calling the method. Assigning a new value to "this"(inside a method) simply decrements the object's reference count.

Code: Select all

var := new A
MsgBox, % var.abc
return

class A
{
	
	__New() {
		this.abc := "Hello"
		this := new B ; does not call A.__Delete as "this" is not the last reference to the object
		this.abc := "World"
		; B.__Delete is called in this case, "this" is the only reference to the object derived from B[class]
	}

	__Delete() {
		MsgBox, % "Instance of class " this.__Class " deleted"
	}
}

class B
{

	__Delete() {
		MsgBox, % "Instance of class " this.__Class " deleted"
	}
}
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Classes...

12 Oct 2013, 16:51

Yeah I know but it'd be difficult to find out that this doesn't work if you're new to ahk.
Recommends AHK Studio
lexikos
Posts: 9679
Joined: 30 Sep 2013, 04:07
Contact:

Re: Classes...

12 Oct 2013, 18:50

Nothing your script does is inherently an error. It depends on the user's intention, which we don't know.

I presume you are suggesting that tes.abc should show 0, for one of the following reasons:

a) this.abc:=0 is intended to affect the original object.
I don't see how anyone could assign this := something and then still expect this to refer to the object it previously contained.

b) tes is intended to contain the new object created by this:=new test2().
The documentation says that you "override the result of the new operator by returning a value". Simply assigning this is not adequate.
Method definitions look identical to function definitions. Each method has a hidden parameter named this, which typically contains a reference to an object derived from the class.
Source: Objects
Like any other parameter, this is an ordinary local variable. Consider this example from the documentation, which shows a method implemented "manually":

Code: Select all

; Create an object.
thing := {}
; Store a value.
thing.foo := "bar"
; Create a method by storing a function reference.
thing.test := Func("thing_test")
; Call the method.
thing.test()

thing_test(this) {
   MsgBox % this.foo
}

Return to “Wish List”

Who is online

Users browsing this forum: burque505 and 8 guests