Preview of changes: scope, function and variable references

Discuss the future of the AutoHotkey language
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Preview of changes: scope, function and variable references

07 Apr 2021, 03:12

@QiuDao The proper way to use quote tags is to include what the other person said inside the tags, and write your own question outside the tags. Your use of the quote tags is very confusing, and your phrasing does not help (I presume they are your words, as they are not mine).

Use whatever approach you want, but value.methodname will not return the method/function if there is also a getter.

Code: Select all

class value {
    static methodname => 'nope!'
    static methodname() => 'return value'
}
MsgBox value.methodname ; nope!
MsgBox value.GetMethod('methodname').Name ; value.methodname
MsgBox value.GetMethod('methodname')(value) ; return value
MsgBox (value.methodname)(value) ; Error (can't call string)
The method definition below creates a property of the same type as target.DefineProp('Method', {call: funcObj}). By default, target.Method returns funcObj and attempting to assign to target.Method throws an error. These defaults can be overridden by defining a property or calling DefineProp.
Source: Objects - Definition & Usage | AutoHotkey v2
If any one of the accessor functions is omitted, behavior is inherited from a base object.
  • An inherited value property is equivalent to a set of accessor functions which return or call the value, or store a new value in this. Note that a new value would overwrite any dynamic property in this itself, and override any inherited accessor functions.
  • If no Set or value is defined or inherited, attempting to set the property will throw an exception.
  • If no Call is defined or inherited, Get may be called to retrieve a function object, which is then called.
  • If no Get is defined or inherited but there is a Call accessor function, the function itself becomes the property's value (read-only).
Source: Object - Methods & Properties | AutoHotkey v2

Given the following, I might remove GetMethod:
  • GetMethod can't handle all ways of defining a method.
  • value.methodname is more convenient.
  • value.methodname works in the common cases.
  • value.methodname is more consistent with functionname and value.methodname().
I left it so far because it shares code with HasMethod and some other things, and HasMethod seems useful enough to keep. Another reason was that Obj.DefineProp(Name, {Call: Fn}) previously did not have a default implementation for get (but does now, as described above).
QiuDao
Posts: 18
Joined: 25 Mar 2021, 22:55

Re: Preview of changes: scope, function and variable references

07 Apr 2021, 08:03

Thank you for reply. Getmethod really has its own use currently. And I will try to be more familiar with the forum as far as possible.
Last edited by lexikos on 08 Apr 2021, 02:19, edited 1 time in total.
Reason: Removed redundant quote
QiuDao
Posts: 18
Joined: 25 Mar 2021, 22:55

Re: Preview of changes: scope, function and variable references

19 Apr 2021, 21:40

I have seen the changes in V2 a130 you've posted on this website:https://www.autohotkey.com/boards/viewtopic.php?f=37&t=2120&start=80#:~:text=Else%20can%20now,the%20value%27s%20cla.
v2.0-a130-c7408b6b
@
04 Apr 2021, 10:26
Changes in behaviour

Changed dynamic properties with a Call function to return the function itself by default when the property is read.
Changed method definitions in classes (and built-in methods) to create dynamic properties with a Call function.
Methods cannot be assigned over by default.
Methods can coexist with property getters/setters.
Changed WinActivate to throw TargetError if the window is not found.
Changed Array.Prototype.Delete to throw if index is out of range.
Changed local/global to allow redeclaring an existing class/function. (Declaring a class/function global was already permitted up until commit a153948c.)
Changed global to allow redeclaring built-in variables.
On the last line of the quoted text, it writes
Changed global to allow redeclaring built-in variables.
But it seems that the built-in variable like A_ahkpath,A_index which acts as a super global variable can never be redeclared. You can use Global in front of a built-in variable without causing an error, but it does nothing different in any context. For example, it still can never be referenced using & even with Global declared ahead.
Is it a bug or just unfinished so it currently does nothing in this change?
Changed global to allow redeclaring built-in variables.
Last edited by QiuDao on 20 Apr 2021, 07:11, edited 2 times in total.
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Preview of changes: scope, function and variable references

20 Apr 2021, 02:01

I don't think I get your point.

It's redeclaring the same variable, the same as any other global declaration when a global variable of any kind already exists, not redefining it.

Right at the moment, I don't remember the reason for that change. It might have been part of the other change, listed separately for clarity.
QiuDao
Posts: 18
Joined: 25 Mar 2021, 22:55

Re: Preview of changes: scope, function and variable references

20 Apr 2021, 06:21

@lexikos
My point is that the global declaration in front of a built-in variable is forever redundant, because you can directly use any built-in variable in anywhere without global declaration. So, is there any difference for a built-in variable between the global declaration existed and not existed?
It might have been part of the other change, listed separately for clarity.
It is not like a part of another change too. All of the changes are below.
Changes in behaviour

Changed dynamic properties with a Call function to return the function itself by default when the property is read.
Changed method definitions in classes (and built-in methods) to create dynamic properties with a Call function.
Methods cannot be assigned over by default.
Methods can coexist with property getters/setters.
Changed WinActivate to throw TargetError if the window is not found.
Changed Array.Prototype.Delete to throw if index is out of range.
Changed local/global to allow redeclaring an existing class/function. (Declaring a class/function global was already permitted up until commit a153948c.)
Changed global to allow redeclaring built-in variables.
Changed DllCall to permit its first parameter to be an object with .Ptr.
Changed __Enum behaviour for Array, Map, Gui and RegExMatchInfo.
Mode of enumeration (which variables are assigned which values) now depends on the first parameter of __Enum, not the passed variables.
This makes it possible for a for-loop to specify a single variable but control which value it gets by placing a comma to either side. Previously Array and RegExMatchInfo always returned the value, regardless of whether the single variable was first or second.
This also changes Gui to return the control object in mode 1 (like Array and RegExMatchInfo), instead of the HWND. Note that mode 1 is also used by Array(enumerable*) and other variadic calls.
Changed the script's main window to keep the script running (persistent) while visible. For instance, a script containing only InstallKeybdHook(), KeyHistory() will not exit until the window is closed.
Changed custom items in A_TrayMenu to not make the script persistent, for flexibility.
Changed OnMessage to not make the script persistent, for flexibility.
Changed OnMessage functions to be able to interrupt the current thread under conditions that previously caused the message to be discarded, such as while a menu is being displayed.
Last edited by QiuDao on 20 Apr 2021, 07:04, edited 3 times in total.
QiuDao
Posts: 18
Joined: 25 Mar 2021, 22:55

Re: Preview of changes: scope, function and variable references

21 Apr 2021, 06:47

Changed HasMethod and GetMethod to verify the method is callable.
Omit param2 to validate param1 itself as a function.
Pass param3 the desired parameter count, for validation.
Omit param3 to only check if the method has a Call method.
I am so excited to see the release of the new version a132 where Hasmethod and GetMethod have get enhanced. But there is a very good advice for Hasmethod I unexpectedly came up with which can truly improve both performance and convenience.
Due to a method is an object which is always true, why not change Hasmethod to just return what the GetMethod return if an object has the method ?
Without this feature, you would probably write:

Code: Select all

if obj.Hasmethod("method1"){
 mthd:=obj.GetMethod("method1")
 ...
}
However, with this feature, you can write:

Code: Select all

if mthd:=obj.Hasmethod("method1"){
 ...
}
Obviously, both the performance and convenience have truly been improved.
As they have shared the code, the only difference between Hasmethod and GetMethod is that when an object has not the given method, one will return true(the function object of the method) while the other will throw an error. Both of them have their own uses.
Also, the feature is unexpectedly compatible with the old because you can just use Hasmethod as before like nothing happened:

Code: Select all

if obj.Hasmethod("method1"){
 mthd:=obj.GetMethod("method1")
 ...
}

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: No registered users and 24 guests