Page 1 of 2
[Vetoed?]Method In OnMessage [I saw the other two posts]
Posted: 20 Aug 2014, 06:15
by RobertL
v2.0-a046-692ef59 wrote:(Link) Disallow methods with OnMessage to avoid confusion.
lexikos wrote:(Link) method's hidden
this parameter
Code: Select all
class c{
m(){
}
static g:=Func("g")
}
g(){
}
OnMessage(0xffff,"c.m")
OnMessage(0xffff,"c.g")
ExitApp
So, they both won't work after v2-0.46!
Please don't disallow it. Maybe it's confusion for some users, but it's also
easy for them to find the answer, and could
maintain their knowledge.
It's a flexible way to create function as method, so why not allow to use them like function.
Otherwise, should we disallow using like
%c.m%(a)?
(maybe they will forget the first param)
Re: Method In OnMessage [I saw the other two posts]
Posted: 20 Aug 2014, 16:29
by lexikos
First: WHY start a new thread?
I don't call what they did before "working". Putting the message function in a class by interpreting this as wParam is a hack, used by people who fool themselves into thinking everything must be contained by the class. Don't do it.
As for it being easy for users to find the answer, I could say the same for most of the other points of confusion which have been removed.
Prohibiting it now allows for potential future enhancements, like automatically calling it as a static method.
Re: Method In OnMessage [I saw the other two posts]
Posted: 20 Aug 2014, 21:05
by RobertL
Eh.....
I thought that thread is finished with a conclusion/common view.
Sorry, I unduly want to emphasize my view, I'm worry for posting there won't be viewed.
Also, I think it's a new/upper stage about the Method In OnMessage.
When user is familiar with this mechanism, it's an
efficient skill, otherwise it would be a
confusion.
This feature is not a
simplified formal
(like traditional method uses the equal sign operator (=) to assign unquoted literal strings), but an necessary
flexible feature.
So, make an
selector switch, something like
#AllowMethodAsMessageHandle true.
automatically calling it as a static method.
calling what?
A
static method means the function defined in class, a
dynamic method means which function is the value of object's key.
Am I correct?
Re: Method In OnMessage [I saw the other two posts]
Posted: 20 Aug 2014, 21:52
by lexikos
RobertL wrote:automatically calling it as a static method.
calling what?
Calling the method, obviously, but passing the class itself to
this and wParam to the first visible parameter.
A static method means the function defined in class, a dynamic method means which function is the value of object's key.
Am I correct?
No, you aren't.
A function becomes a method when it accepts "this" object as its first parameter. All functions defined inside classes do. The difference between a static method and a non-static method is merely whether "this" object refers to the class itself or a derived object. A method can easily work both ways, by checking
if (this = MyClass).
Storing a function reference in an object is also possible. If you call it with
%object[key]%(), no parameters are passed - it is not a method. This usage will work with OnMessage as long as the function is not defined inside a class.
but an necessary flexible feature.
No, it isn't necessary. It allows flexibility in that you are free to write code that makes no sense. I don't value that kind of flexibility very highly. There are better, clearer ways to achieve a similar result.
Re: Method In OnMessage [I saw the other two posts]
Posted: 20 Aug 2014, 22:18
by RobertL
Understand now.
like automatically calling it as a static method. means call a method like a function.
passing the class itself to this and wParam to the first visible parameter
Pass
class to extra hidden parameter
this, which user can ignore this
this, using a method like a noraml function.
I don't like it, I will think about it later.
A function becomes a method when it accepts "this" object as its first parameter.
No, I think it's
(become method) when define it
(function) in class.
Then an extra hidden
this will be the actual first parameter.
I
had thought your
non-static method is what I mean
which function is the value of object's key (having normal first parameter).
non-static method. I see,
the class itself or a derived object.
I think it's an new but unnecessary/useless
concept.
Just think the method as a function with strange first parameter.
My
dynamic method is (similar like?)
function reference in an object.
Skip these difference views above, I think it's less important.
This usage will work with OnMessage as long as the function is not defined inside a class
I want to define
(and use) function in class as
method.....Also in
OnMessage.
I use
class to maintain
function.
Although a
static method's function name
MyClass.Method has less difference from
MyClass_Method.
necessary/unnecessary flexible feature
It make sense...Many account
(I can't remember now/I will prove/find),
see example in this post at Support load method call without #include (not really suitable)
Re: Method In OnMessage [I saw the other two posts]
Posted: 21 Aug 2014, 03:34
by lexikos
RobertL wrote:No, I think it's (become method) when define it (function) in class.
Are you saying that even if a function has exactly the same usage and behaviour as a method, it's still not a method if it wasn't defined inside a class? What of prototyped objects, which have no class definition?
You asked if you were correct, and then argued when I gave you the answer. This does not encourage me to answer you in future.
Re: Method In OnMessage [I saw the other two posts]
Posted: 21 Aug 2014, 03:43
by nnnik
The question if he was right was a rhetorical question.
Re: Method In OnMessage [I saw the other two posts]
Posted: 21 Aug 2014, 05:10
by RobertL
Sorry, I didn't find my way,
No perhaps means
What! I'm wrong again? or
No! There still are some different view.
I think the definition between
method and
function is useless.
Just think the method as a function with strange first parameter.
But I still want to give my option.
- Physically, a method is a function defined in an class, which have a hidden first parameter this. Also it's function name contain name of its parent class, there is a pseudo-keyword base, last class object is super-global.
How a method is defined.
- Logically, a function which is the value of an object's key and use object-syntax to call is a method.
How a function is used/called.
So
a function has exactly the same usage and behaviour as a method, it's still not a method in first case.
Won't be exactly the same, see below IsFunc.
So I ask
Is a static method means the function defined in class, a dynamic method means which function is the value of object's key.(They are
different method)
IsFunc("c.m") -
static method,
IsFunc("o.m") -
dynamic method(function reference in an object).
And MyFunction(AnotherObjectAsFirstParameter) is not a method in any way.
Sorry, I'm trying to adjust some of my concept. And sometimes, I confuse them.
Thanks for all of your discussion, which have a civilizing influence on my view.
I think our points are basically the same with different aspects.
Now where were we? Ha, what about recover
Method In OnMessage or make a selector switch.
You make the choice, I know my demand is not necessary, just for some convention
(Package everything in class)
Re: Method In OnMessage [I saw the other two posts]
Posted: 21 Aug 2014, 09:27
by guest3456
When I think of "method", I think of a function that belongs to an object and acts on the object's own data.
In BOTH of these examples,
.test() is a 'method':
Code: Select all
thing := {}
thing.foo := "bar"
thing.test := Func("thing_test")
thing.test()
return
thing_test(this) {
MsgBox % this.foo
}
Code: Select all
thing.test()
return
class thing
{
static foo := "bar"
test()
{
MsgBox % this.foo
}
}
Those objects are identical as far as I know?
I think the class syntax confuses people because they try to encapsulate everything into a class, and they think that all objects are classes, as if class syntax is the only way to create an object. Earlier discussions talked about using the
prototype keyword instead of
class, I don't know if that would have helped or not. I also think the equivalent example above or even something more elaborate should be on the docs page for objects so people can compare what the class syntax really translates into.
Re: Method In OnMessage [I saw the other two posts]
Posted: 21 Aug 2014, 19:50
by RobertL
guest3456 wrote:I think of a function that belongs to an object and acts on the object's own data.
Almost. I think it's hard to
define, and useless to
discriminate.
See
my post one stair above, contain some more case.
Those objects are identical as far as I know?
Not actually I think. Also see that post, there is a
class and a
prototyped object.
Class syntax(Object syntax) is a flexible way with expansibility. See the example at
this post.
I don't discriminate much between
prototype and
class. Both OK, just different
aspect.
I think the example is not equivalent.
class is a predefine way/keyword to create a
static base, but a class is not exactly equal to an
object.
Discussion now is about
method in static
class and non-static/dynamic
object. Long and hard to follow..
Maybe forget the unnecessary difference is better.
Re: Method In OnMessage [I saw the other two posts]
Posted: 21 Aug 2014, 21:16
by lexikos
RobertL wrote:Those objects are identical as far as I know?
Not actually I think.
Not identical, but very close.
Code: Select all
global thing ; Classes are "super-global" by default.
thing := {}
thing.__Class := "thing" ; Set automatically by class syntax.
thing.foo := "bar"
thing.test := Func("thing_test")
thing.test()
return
thing_test(this) { ; Function name has _ instead of .
MsgBox % this.foo
; base.test() ; Not available here.
}
class is a predefine way/keyword to create a static base, but a class is not exactly equal to an object.
That depends on which "class" you're talking about.
class thing in guest3456's example constructs an object almost identical to the one above. If you're talking about the concept of a "class", you could say that the code above constructs one without using "class syntax". They are just different ways of expressing the same idea, and achieving the same end result.
Also see that post, there is a class and a prototyped object.
...
Maybe forget the unnecessary difference is better.
Uhm, yes? Forget there's a difference between a "class" and a "prototype" because the distinction is unnecessary: in AutoHotkey, they are the same thing.
Re: Method In OnMessage [I saw the other two posts]
Posted: 21 Aug 2014, 21:45
by RobertL
@Lexikos Hope you didn't miss
that that post above. Only need to see the
last paragraph (About the conclusion/target of this thread/topic).
Re: Method In OnMessage [I saw the other two posts]
Posted: 22 Aug 2014, 10:56
by evilC
OK, I don't profess to fully understand this subject yet, but I have to agree with the sentiment that it would be nice to have OnMessage expanded to allow it to be neatly encapsulated in a class.
I know Lexikos has his reservations about the need for such a thing, but for messages such as scrolling (0x114/115) it would be really nice as the HWND passed as the 4th param for OnMessage is not always the HWND of the window you wish to route the scroll message to, and class encapsulation is a nice way to handle this.
For example, when trapping rolls of the mouse wheel, you do not get the HWND of a window. The logical thing to do is find the HWND of the item under the cursor, but this is not always the window to be scrolled - eg if you scroll a window with a child control - as you scroll, the child control may pass under the mouse cursor and so you would get the HWND for the control.
So if you could encapsulate all gui items (controls, guis) into classes, you could easily have the class for non-scrollable gui countrols (eg edit boxes, checkboxes) pass the onscroll message to it's parent.
I have coded this using alternative methods (Classes register themselves with a "Message Handler") but it just makes stuff more complicated.
Would it not be possible to add an optional 4th parameter to OnMessage, like so?
Code: Select all
class c {
__New(){
[...]
; Suggested syntax:
; The extra "this" param specifies what the "hidden this" param passed to OnScroll will be
; And also which object to call the OnScroll method of.
OnMessage(0x114,"OnScroll",, this)
OnMessage(0x115,"OnScroll",, this)
}
}
Re: Method In OnMessage [I saw the other two posts]
Posted: 22 Aug 2014, 20:55
by RobertL
@evilC I'm not quite understand. It's a new subject.
Does A_Gui/A_GuiControl.. can be used when 0x114/WM_HSCROLL?
Also, some message about control in GUI will be encapsulate in /pass through WM_COMMAND.
Support the window - control father - children relation object directly would be easier.
In this topic, I want encapsulated message handle function as method, so OnScroll function will be c.OnScroll.
Re: Method In OnMessage [I saw the other two posts]
Posted: 23 Aug 2014, 11:22
by evilC
I don't think A_Gui or A_GuiControl hold anything in V2.
Personally, I use
MouseGetPos(tmp,tmp,tmp,hwnd,2) to retrieve the HWND of what is under the cursor.
My point was that the code
OnMessage(0xffff,"c.g") would probably not behave how you want.
In your example, c is the class prototype, not an instance of a class.
When c.g is executed, what is "this" set to?
For example, consider this code:
Code: Select all
Class c {
[...]
}
OnMessage(0xffff,"c.OnScroll")
MyClassInstance := new c()
When the onscroll method is called, it would call c.OnScroll, not MyClassInstance.OnScroll
Which is why I proposed a syntax like this:
Code: Select all
Class c {
[...]
}
MyClassInstance := new c()
OnMessage(0x114,"OnScroll",, MyClassInstance)
The extra parameter "MyClassInstance" tells AHK three things:
1) Call OnScroll as a class method, not a regular function.
2) Call it on the MyClassInstance object.
3) The "hidden this" parameter passed to MyClassInstance.Onscroll should be MyClassInstance.
Re: Method In OnMessage [I saw the other two posts]
Posted: 23 Aug 2014, 18:16
by lexikos
evilC wrote:I don't think A_Gui or A_GuiControl hold anything in V2.
Why would you think that? If they had changed in any way, it would be mentioned in
v2-changes.
Re: Method In OnMessage [I saw the other two posts]
Posted: 23 Aug 2014, 19:19
by evilC
I think I got myself confused.
In the context of window scrolling messages, in order to route mouse wheel to the normal scroll routines, I did like you did in your scrolling proof of concept and added a hotkey for wheel up and wheel down, which calls the same routine that would be called if you dragged the handle. Hence my comment about using MouseGetPos to get the HWND of the item under the cursor.
In this case, A_Gui is obviously not set, as the scroll was not due to gui interaction.
Apologies for causing confusion.
Re: Method In OnMessage [I saw the other two posts]
Posted: 23 Aug 2014, 20:18
by guest3456
evilC wrote:
In your example, c is the class prototype, not an instance of a class.
Theres no such thing as an instance of a "class" in AHK. There are "objects" which can be defined in many ways, one of which is using the 'class' syntax.
And yes a class definition is an instance of the object. Thats how prototype based oop works. You first create an object. Then you can create new objects that use the original object as its 'prototype' or 'base'
Re: Method In OnMessage [I saw the other two posts]
Posted: 24 Aug 2014, 04:40
by RobertL
My point was that the code OnMessage(0xffff,"c.g") would probably not behave how you want.
In your example, c is the class prototype, not an instance of a class.
I want to
encapsulated function in class as
method. The message handle function(method) is
not concerned with
class nor
instance.
The hidden parameter
this is just the first real parameter passed to message handle function.
If you want transmit more information to it, use pseudo-keyword
base.
We should deliver message by ourselves when OnMessage. Route it in handle function.
Re: Method In OnMessage [I saw the other two posts]
Posted: 24 Aug 2014, 21:00
by lexikos
guest3456 wrote:Theres no such thing as an instance of a "class" in AHK. There are "objects" which can be defined in many ways, one of which is using the 'class' syntax.
You're wrong. Those objects are also instances of the class they are derived from.