Lexikos wrote:
When the user defines a function, the function name is stored for use with dynamic function calls, Func.Name, ListVars, A_ThisFunc, etc. When the user defines a function inside a class definition, the final function name includes both the class name and the name specified by the user.
Lets suppose we have three methods:
Code: Select all
class cx {
chicken() {
}
}
class cy {
static cow := Func("cy_cow")
}
cy_cow(this) {
}
class cz {
static lion := Func("cz_barn")
}
cz_barn() {
}
In those examples, the names of _methods_ would be:
- "cx.chicken"
- "cy_cow"
- "cz_barn"
and would not be:
"cx.chicken"
- "cy.cow"
- "cz.lion" or "cz_lion"
Lexikos wrote:
A "method" in AutoHotkey consists of multiple parts:
- The function which implements it.
- The key-value pair which associates the target object/class with the function.
- The concept which ties it all together.
Since this discussion is about Func(), which only knows about functions, not methods, I've mostly used "method" to mean #1.
Example1: if I define a function named
"cy_cow" defined somewhere in the script, then
"cy_cow" is the name of the _function_. If I additionally have an object with a key and the value of that key holds the reference to the function
"cy_cow", then
"cy_cow" is also a name of the _method_?
Example2: if _I_ define a function named
"chicken" inside of the class object whose name is
"cx" then the compiler will create a function named
"cx.chicken" and
"cx.chicken" will be the name of the _function_. If I additionally have an object with a key and the value of that key holds the reference to the function
"cx.chicken" (the object doesn't necessary being the object
"cx"), then
"cx.chicken" is _also_ a name of the _method_.
This is how I understand the definition of the word 'method'.
Lexikos wrote:
naming conventions for functions and for variables are different (names of variables can't contain a dot)
No. Variable, function, class and window group names all follow the same rules. However, the rules only apply to what the _user_ types when defining the method/function.
Note: _user_.
Lexikos wrote:
The name of the function sort of then becomes a container that holds a reference to the function and the _value_ of that container can't be changed. Thanks to the fact that the name of the container cant' be changed, (...)
The script internally keeps a list of all functions which have been defined, including methods. Users can't access this list directly.
Because all methods have to have a function definition and that function definition has to have a name that is the name of the function, _all_ names of methods are names of functions?
Lexikos wrote:
I can also be sure that by using the name of the function, I'll always refer to that particular function, even if the key which held a reference to the function is assigned a different value.
No. You cannot be sure that Func() will return a _method_
If the parameter of
Func() is a string that is the name of the function, then
Func() will return the reference to the function. Because a method is:
- a function definition (and part of that definition of the function is the _name_ of the function)
- a key of the class object whose value holds the reference to the defined function
=> a method is always a function. So if I pass the name of the _method_ (which is at the same time the name of the function) to
Func(),
Func() will return a _function_ reference. If I pass a name of the method to
Func() I _can_ be sure that
Func() will return [a reference to] a method (because I know that I've passed a method to
Func() ). But, if I _don't_ know if the name of the function that I pass to
Func() is the name of the _method_, I _can't_ be sure that
Func() will return a reference to a _method_. This is how I understand this.
So it's not that
Func() _can't_ return a reference to the _method_, it's rather that if I pass a name of the function to
Func(), I can't be sure if the reference to the function returned is also a reference to the method? (because I don't know if the name of the function that I passed to
Func() was a method in the first place)?
Lexikos wrote:
No. You cannot be sure that Func() will return a _method_, because it isn't documented. It isn't documented because it isn't intended for use; it is just a consequence of the implementation. This is what I meant when I said "IsFunc("cx.method") is not guaranteed to return non-zero."
Lets clear this up. There are two meanings one could derive from the word 'method' in "cx.method":
- "cx.method" is treated as the name of the method (and at the same time the name of the _function_) _literally_, character by character, just like "cy_cow" or "cx_lion"
- 'method' in "cx.method" is a placeholder for the name of any
function name of the key, that (the key), when combined with the prefix "cx", ("cx" + "name of key") gives the name of the function that (the function) was defined inside of the body of the class that (the method) also happens to be part of (of the class)
To clarify, what I've meant by
"cx.method" is the _former_ (just a literal name of the _method_).
Now, if I've meant the former, then is it correct that _even_ for functions that are defined _inside_ of the class and that are methods,
IsFunc() _is_ guaranteed to return non-zero (because what I'm passing to
IsFunc() is still a name of the _function_ (regardless of whether that function is a name of the method or not).
I don't seem to understand in what cases
IsFunc() is _not_ guaranteed to return non-zero. Even if the argument of the function is the name of the function that was defined inside of the class, then, because I pass the name of the _function_ to
IsFunc(),
IsFunc() has to guarantee a non-zero result, because what I'm passing is the name of the _function_ (which also just happens to be the name of the method).
Lexikos wrote:
No. Keys can be integers, strings or references - this is documented. In cx.method, "method" is a key. You could also call it a method name, in that it is the name you use to refer to/call the method. However, this [the word "method" in the string "cx.method"] isn't what I'm talking about when I say the method's name, since I'm talking about the method/function itself, independent from "cx".
So, if the word 'method' in
"cx.method" is treated _literally_ as the name of the method, just like
"cx.chicken", then the name of the method is
"cx.method" and not just
"method", just like the name of the method is
"cx.chicken" and not just
"chicken"? This is what I've meant originally. whenever I wrote
"cx.method" - I haven't intended the word 'method' to be a placeholder word. I should have quoted the whole string probably to remove the ambiguity.
Lexikos wrote:
Func() _could_ differentiate between of whether this string is the name of the function or whether the string is the name of the method that should be resolved 'dynamically'
You're right; it could. A more likely use for this is to prevent Func() from ever returning
a function which was defined inside a class. However, that could have a negative (but very small) impact on code size and performance; hence why it is the way it is.
So
Func() supports as the parameter:
- names of functions that aren't methods
- names of functions that are methods defined outside of the class
but does _not_ support names of functions that are methods defined _inside_ of the class? (
Func() just returns a correct value of those because of the current implementation but I shouldn't rely on that value). If that is true, then it would be _false_ to say that
Func() doesn't work with methods? (because
Func() _has to_ work with names of functions that (the functions) are additionally names of methods; methods defined outside of the class).
I clearly lack the understanding of something there.