Defining Class Members (in vs out of constructor) Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Coiler
Posts: 75
Joined: 29 Nov 2020, 09:06

Defining Class Members (in vs out of constructor)

Post by Coiler » 16 Dec 2020, 11:01

I have a very basic question that I'm having trouble searching/finding an answer to. Are the following two classes identical?

Code: Select all

class Car
{
	Color := "Red"
	Speed := 50
	Brake := 10
}

Code: Select all

class Car
{
	__New()
	{
		this.Color := "Red"
		this.Speed := 50
		this.Brake := 10
	}
}
I'm familiar with languages like Python, where they would not be the same. The docs appear to be showing that they probably are equivalent in AHK, but I wanted to make sure.

mcl
Posts: 86
Joined: 04 May 2018, 16:35

Re: Defining Class Members (in vs out of constructor)

Post by mcl » 16 Dec 2020, 11:40

Your first example contains instance variables and is equivalent to:

Code: Select all

class Car
{
	__Init() {
		this.Color := "Red"
		this.Speed := 50
		this.Brake := 10
	}
}
When you create class instance, __Init will be called first, __New will be called next. So, unless you rewrite these values in __New, results in both examples will be the same.

Static variables, however, belong to class. You can refer to them without creating an instance, and instances inherit them by prototype chain.

Code: Select all

class Car
{
	Static Color := "Red"
	Static Speed := 50
	Static Brake := 10
	
	__New() {
		; ...
	}
}
Msgbox % Car.Color  ; "Red"
x := new Car()
Msgbox % x.Color  ; "Red"
Msgbox % x.HasKey("Color")  ; False
Car.Color := "Blue"
Msgbox % x.Color  ; "Blue"

swagfag
Posts: 4126
Joined: 11 Jan 2017, 17:59

Re: Defining Class Members (in vs out of constructor)  Topic is solved

Post by swagfag » 16 Dec 2020, 11:46

depends. identical by what metric? after having been constructed, instances derived from these classes can be considered identical - both will contain the same named fields with the same values. its the method by which this was achieved that differs.

in ur first example, the variables are set by the ahk-generated __Init method, which runs prior to any constructors(in this case there arent any though).

in the second example no __Init is generated and run, instead the constructor takes on the responsibility for setting the variables.

however, none of this should matter greatly in the general case. its only when u get down to very specific things, u might wanna do in the constructor, that it could start mattering(eg the constructor relying on certain member fields existing/not existing)

User avatar
Coiler
Posts: 75
Joined: 29 Nov 2020, 09:06

Re: Defining Class Members (in vs out of constructor)

Post by Coiler » 16 Dec 2020, 20:28

I think it is the title "instance variables" that keeps throwing me off. My brain keeps wanting me to see something with that title as a class-instance variable. As in an instance of the class itself, rather than one of its members. Obviously that wouldn't make sense with my example, where int and string values are being assigned. But I think the docs just had something like InstanceVar := Expression.

I just wanted to make sure that the class instances in both of these examples would have the same member-type setup. I remember coding a lot of python before I eventually realized that my classes all (unintentionally) had static members, and I was dynamically adding non-static versions of everything when I modified the values. Just trying to avoid repeating mistakes.

Both of your answers are good. I only chose swag because he spelled it out for me: "they can be considered identical"

Thanks!

Post Reply

Return to “Ask For Help”