Classes in AHK, a Dissection (Advanced)

Helpful script writing tricks and HowTo's
-_+
Posts: 70
Joined: 06 Dec 2014, 12:43

Re: Classes in AHK, a Dissection (Advanced)

26 Oct 2015, 20:36

lexikos wrote:You asked:
can I use the same newobj := new myClass() [...]?
In that case, newobj is already an instance of myClass and you can just call one of its normal methods, as I said.

Ok, I got. Since you didn't answer the questions from my next message I take it as there is no way to apply a class's method to an object that was created not by an instance of a class.
I hoped that classes could be used like that:

Code: [Select all] [Download] GeSHi © Codebox Plus

myObject := new myClass()
myObject := ["a", "b"]
myObject.myMethod() ; it should become ["a", "b", "c"], but instead it's ["a", "b"]. If one swaps first two lines - the result becomes ["c"].

class myClass
{
myMethod()
{
this.Push("c")
}
}

But seems like they can work only with objects created inside of them, and thus they can't do what can be done using just functions. Well, you'll once again would say that I'm actually wrong and one can use them like this:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


But that's just an overcomplication compared to this:

Code: [Select all] [Download] GeSHi © Codebox Plus

myObject := ["a", "b"]
myFunction(myObject)
MsgBox, % (myObject.MaxIndex() == 3 ? "true" : "false" ) ; true, myRealObject is now ["a", "b", "c"]

myFunction(ByRef input)
{
input.Push("c")
}

In the above examples I populate an object manually, but that's not how it usually happens, so there's no point claiming that I could have used

Code: [Select all] [Download] GeSHi © Codebox Plus

...
class myClass
{
__New()
{
this.Push("a", "b") ; by the way, I'd use here 'this := ["a", "b"]' instead, but that won't work.
}
...

So, for the case when you need to alter an existing object populated with some data - classes and methods are of no use. Right?
lexikos
Posts: 6130
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Classes in AHK, a Dissection (Advanced)

26 Oct 2015, 21:34

-_+ wrote:Ok, I got. Since you didn't answer the questions from my next message I take it as there is no way to apply a class's method to an object that was created not by an instance of a class.
Unless I've missed something, you have never once, until this post, actually mentioned or demonstrated this requirement.

Anyway, I did answer the question:
I wrote:... use the Call method, as in SomeClass.SomeMethod.Call(newobj) (or rethink your design).

So, for the case when you need to alter an existing object populated with some data - classes and methods are of no use. Right?
Perhaps. When I said "(or rethink your design)", my intention was to draw into question the validity or usefulness of this approach -- specifically, defining methods in a class and then not making the object an instance of that class. In that case, why not just use a function?

If using a class provides better organisation, then you can use a "static" method which accepts the object as a parameter, as in SomeClass.SomeMethod(newobj).

myObject := ["a", "b"] obviously creates a new object and replaces the previous one. It would be more sensible to do this:

Code: [Select all] [Download] GeSHi © Codebox Plus

myObject := new myClass()
myObject.Push("a", "b") ; not: myObject := ["a", "b"]
myObject.myMethod()

This would achieve the same result (not recommended):

Code: [Select all] [Download] GeSHi © Codebox Plus

myObject := ["a", "b"]
myObject.base := myClass ; myObject is now "an instance of" myClass
myObject.myMethod()

This would achieve almost the same result (not recommended):

Code: [Select all] [Download] GeSHi © Codebox Plus

myObject := ["a", "b"]
myClass.myMethod.Call(myObject)
-_+
Posts: 70
Joined: 06 Dec 2014, 12:43

Re: Classes in AHK, a Dissection (Advanced)

27 Oct 2015, 14:01

Thanks for your answers, Lexikos, it seems way more clear now to me.
But why did you name the last two examples not recommended? Is it because of just a bad/not obvious workflow or are there any hidden catches?
lexikos
Posts: 6130
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Classes in AHK, a Dissection (Advanced)

27 Oct 2015, 16:16

If you have to use either, it probably reflects a mistake in your design. If you set 'base' manually, you get "an instance of myClass" for which the constructor has not been called and instance variables have not been initialized. If you use .Call(), the method cannot call other methods within the class with this.methodName(), because this is not an instance of the class. Either point could come back to bite you, and there's just no good reason to risk it - there's nothing wrong with passing an object as a parameter.
-_+
Posts: 70
Joined: 06 Dec 2014, 12:43

Re: Classes in AHK, a Dissection (Advanced)

27 Oct 2015, 16:51

Ok, got it, thanks for the explanation.
wizardzedd
Posts: 319
Joined: 23 Jan 2016, 23:03

Re: Classes in AHK, a Dissection (Advanced)

09 Feb 2016, 12:08

Nice post GeekDude! Classes in ahk have always been a bit of a mystery to me, but now I feel like I can go dig into them. It is definitely different from the classes in strongly typed OOP languages.
User avatar
lmstearn
Posts: 149
Joined: 11 Aug 2016, 02:32
GitHub: lmstearn

Re: Classes in AHK, a Dissection (Advanced)

16 Aug 2016, 02:29

Yes, thanks for the info,
For the sake of practicality, (at least in C likes) a class is just
Class Phoo
{
//Member list
}
A little more on Metas in the Tutorial: If any of these (except __Init()) are omitted from the base class, are there generic routines invoked instead?
Nice little example of polymorphism might be worth a mention.
For further reading on more advanced issues this is a fabulous continuation.
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
lexikos
Posts: 6130
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Classes in AHK, a Dissection (Advanced)

16 Aug 2016, 03:14

lmstearn wrote:A little more on Metas in the Tutorial: If any of these (except __Init()) are omitted from the base class, are there generic routines invoked instead?
If there were, they would be empty and do nothing. Meta-functions are for implementing custom behaviour. Merely defining or not defining the meta-function makes no difference; it is what the meta-function does that matters.
User avatar
lmstearn
Posts: 149
Joined: 11 Aug 2016, 02:32
GitHub: lmstearn

Re: Classes in AHK, a Dissection (Advanced)

16 Aug 2016, 08:48

lexikos wrote:If there were, they would be empty and do nothing. Meta-functions are for implementing custom behaviour. Merely defining or not defining the meta-function makes no difference; it is what the meta-function does that matters.

If there is the same meta method defined for each base in object.base1.base2.base3.Meta, the inbuilt meta is performed first, followed by the base1 or base3 method?
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
lexikos
Posts: 6130
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Classes in AHK, a Dissection (Advanced)

16 Aug 2016, 18:35

I suggest you read the manual, because your question makes no sense to me.

Return to “Tutorials”

Who is online

Users browsing this forum: icemanmelb and 3 guests