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.
__New/__Init Questions
Re: __New/__Init Questions
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.
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.
Re: __New/__Init Questions
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:
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:
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.
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
}
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
Re: __New/__Init Questions
1) right!
2) I see no reason to implement __init explicitly in a class definition.
3) Example,
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!
Re: __New/__Init Questions
Got it. This helps a lot. Thanks Helgef!
Re: __New/__Init Questions
(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"
}
If the property takes its value from a parameter of __New, there may be no point in predefining it.
Re: __New/__Init Questions
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.
Re: __New/__Init Questions
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.
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.
Re: __New/__Init Questions
Yes, wait until you learn java