Is possible to create a class with constructor overloads? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Ralf_Reddings200244
Posts: 94
Joined: 11 Mar 2023, 14:16

Is possible to create a class with constructor overloads?

Post by Ralf_Reddings200244 » 08 Apr 2024, 17:31

For example if I have a class that constructs a Timecode object, I would like to sometimes initialise a instance by just providing a value for a single parameter (frameCount) and other times provide more than one parameter such as frameCount and frameRate.

Here is what I tried:

Code: Select all

obj := new timecode(2)			;Default to using 12 frames per second
print(obj)							;prints ---> {"frameCount": 24, "framerate": 12}
obj := new timecode(2, 24) 	;Use the provided frame rate 
print(obj)							;prints ---> {"frameCount": 48, "framerate": 24}


class timecode
{
	__New(frames)
		{
			this.frameCount := frames * 12
			this.framerate := 12
		}
	__New(frames, framerate)
		{
			this.frameCount := frames * framerate
			this.framerate := framerate
		}
}
but the above just throws an error:

Code: Select all

 ==> Duplicate declaration.
     Specifically: __New
AutoHotkey closed for the following exit code: 2
I know that V1 is as dead as latin, and V2 classes are much better now but until the library porting for V2 catches up, I will be on V1.

Any help would be greatly appreciated!

ShatterCoder
Posts: 77
Joined: 06 Oct 2016, 15:57

Re: Is possible to create a class with constructor overloads?  Topic is solved

Post by ShatterCoder » 09 Apr 2024, 12:53

the closest ahk comes to what you are thinking of is default values. Rather than defining constructors for each set of values you can set defaults for a single constructor. The reason ahk does not have overloads becomes more apparent when you consider that ahk is not a strongly typed language. Meaning it could care less about weather the var in your constructor def is a string, int, double, char etc. Because of this, ahk has no way of knowing anything about the vars passed to the constructor other than their order. so that being said what ahk can offer here is the following:

Code: Select all

obj := new timecode(2)			;Default to using 12 frames per second
print(obj)							;prints ---> {"frameCount": 24, "framerate": 12}
obj := new timecode(2, 24) 	;Use the provided frame rate
print(obj)							;prints ---> {"frameCount": 48, "framerate": 24}
return

class timecode
{
;simple default style argument def, if you pass a value it overrides otherwise framerate uses the default value of 12
;side note, ahk does not allow you to put a non default value after one that is defined with default value, so you could not add a 3rd argument unless you assigned it a default value as well, or added it before framerate
	__New(frames, framerate := 12) 
		{
			this.frameCount := frames * 12
            this.framerate := framerate
		}
}

Ralf_Reddings200244
Posts: 94
Joined: 11 Mar 2023, 14:16

Re: Is possible to create a class with constructor overloads?

Post by Ralf_Reddings200244 » 11 Apr 2024, 11:06

@ShatterCoder
Thanks, I am glad to know conclusively and the reasoning behind it too. Cheers!

Post Reply

Return to “Ask for Help (v1)”