__New/__Init Questions

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
sirksel
Posts: 222
Joined: 12 Nov 2013, 23:48

__New/__Init Questions

02 Jun 2020, 21:24

1. Class body instance var declaration vs. New() assignments: Is there any reason (aside from documentation) to initialize a (non-accumulating) instance variable in the class body that's going to be assigned anyhow in __New() based on a parameter?

2. __Init: Is there any special functionality that could be achieved by using __Init() over just a class-body instance variable declaration? In some languages, I know __init and/or __new can prevent the creation of the object. I don't think either has that capability here, right? Anything else interesting that __Init() can do?

3. Preventing instantiation: In most of my code, I've settled on one of two ways to prevent instantiation if an error condition is met. One is I throw an exception. The second is I use a static method or function as an alternate constructor that calls __New() if and only if no error condition is present. Is there any other more idiomatic way to prevent instantiation -- say to return "" on error, when such an error warrants only falsey evaluation rather than runtime error?

Any thoughts? Thanks.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: __New/__Init Questions

02 Jun 2020, 22:51

1. I guess not, but I'm not sure I understand the question.
2. You shouldn't explicitly define __init in a class definition. It is automatically defined. When __init/__new is called the object has already been created.
3. Using static new is the way to go.

Cheers.
sirksel
Posts: 222
Joined: 12 Nov 2013, 23:48

Re: __New/__Init Questions

03 Jun 2020, 02:13

Excellent. Thanks, Helgef. I'm just cleaning up code as I'm transferring it to the latest alpha, so I'm trying fix little issues as I see them.

1. To clarify my question just a bit, there's no reason you'd ever set _x in both places:

Code: Select all

class test {
	_x := 0   ;this just slows things down, right?
	_y := 0 
	__new(x) {
		this._x := x
	} ;... and other methods/props that later deal with _y
}
2. When the documentation says "Therefore, a single class definition must not contain both an __Init method and an instance variable declaration" -- I was just wondering what edge case might have me needing to implement __Init. Sounds like there isn't one that readily comes to mind. If you can't think of one after all the work you've done in ahk, I'll probably never come upon one!

3. I'm proposing some code here, to go along with the test class above. Can you modify to show me what you mean about the static new approach?

So let's say I want a negative x value to always result in ""/falsey rather than creation of the object, so I can test it in an if statement. (The actual validation might be considerably more complicated...) Let's say it's not worth exception overhead because its an expected condition (maybe coming from user input or something). Here's how I might use the alternate passthrough constructor:

Code: Select all

safetest(x) => x<0 ? '' : test.new(x)  ;there's no way to validate in __New or __Init
if (t := safetest(3))
	msgbox type(t)   ;results in an object
if (t := safetest(-3))
	msgbox type(t)   ;never executes since t is an empty string
Is this the right approach? Were you thinking of the static new approach for a singleton construction, or does it also apply to something like this? Thanks for the help.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: __New/__Init Questions

03 Jun 2020, 02:28

1) right!
2) I see no reason to implement __init explicitly in a class definition.
3) Example,

Code: Select all

class neg {
	__new(x) {
		; here (this is neg)
		this.x := x
	}
	static new(x) {
		; here (this == neg)
		if x >= 0
			throw
		return base.new(x) ; creates the new object, calls __init and __new
	}
}

neg.new -1 ; Ok!
neg.new 1 ; throws!
sirksel
Posts: 222
Joined: 12 Nov 2013, 23:48

Re: __New/__Init Questions

03 Jun 2020, 03:45

Got it. This helps a lot. Thanks Helgef!
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: __New/__Init Questions

04 Jun 2020, 23:43

sirksel wrote:
02 Jun 2020, 21:24
Is there any reason (aside from documentation) to initialize a (non-accumulating) instance variable in the class body that's going to be assigned anyhow in __New() based on a parameter?
(I may be missing the point of "non-accumulating".)

If __New is overridden by a derived class, any properties defined by the base class version of __New will not be available until it calls base.__New().

You can override a property value via an initializer (which really just translates to an assignment) and have it affect the construction process performed in __New. Something like this:

Code: Select all

class InsensitiveMap extends Map {
    CaseSense := "Off"
}
(although there's really no __New in this case).

If the property takes its value from a parameter of __New, there may be no point in predefining it.
iseahound
Posts: 1445
Joined: 13 Aug 2016, 21:04
Contact:

Re: __New/__Init Questions

05 Jun 2020, 20:13

You would use __Init to define a contravariant class. For example, if you want to overwrite the current method with the base method (from an extended class) you could do so here.
Flowgun
Posts: 75
Joined: 25 Aug 2022, 09:42

Re: __New/__Init Questions

25 May 2023, 21:11

I haven't dabbled much in Autohotkey's OOP, but since Autohotkey is what I learned programming with, I read many tutorials about it. Then I started learning Python.
I'm finding many things confusing. "__init" in Autohotkey is somehow equivalent to "__new__" in Python (runs first, and almost never used), while "__new" in Autohotkey is equivalent to "__init__" in Python (runs after, and is responsible for the object's initialization).

Besides that, what Python calls Dunder properties/special methods/magic functions is equivalent to Autohotkey's "meta-functions", but it's not the same as meta-classes.

:headwall: :headwall: :headwall: :headwall:
iseahound
Posts: 1445
Joined: 13 Aug 2016, 21:04
Contact:

Re: __New/__Init Questions

26 May 2023, 02:03

Yes, wait until you learn java :)

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: mikeyww, Rohwedder, sanmaodo, Xtra and 49 guests