「機能オブジェクト」とは、通常、以下のいずれかを意味する:
ファンクション・オブジェクトは以下のように使用できる:
オブジェクトが呼び出し可能かどうかを判断するには、以下のいずれかを使用する:
Value.HasMethod()は、デフォルトではすべてのAutoHotkeyの値とオブジェクトで動作しますが、一部のオブジェクトやクラスではHasMethodをオーバーライドできます。COMオブジェクトの場合、COMオブジェクトが実際には別のプロセスのAutoHotkeyオブジェクトでない限り、これは通常失敗します(例外をスローするか、間違った結果を生成します)。HasMethod(Value)は、すべてのAutoHotkeyの値およびオブジェクトで動作し、オーバーライドすることはできませんが、Callメソッドの存在を判断できない場合はfalseを返します。Valueが ComObjectの場合は例外がスローされる。ユーザー定義関数オブジェクトは、「関数」の実装を含むCallメソッドを定義しなければならない。
class YourClassName {
Call(a, b) { ; Declare parameters as needed, or an array*.
;...
return c
}
;...
}
これは、YourClassName()が返すオブジェクトなど、YourClassNameのインスタンスに適用されます。Callを static Callに置き換えると、代わりにYourClassName自身が呼び出されたときに起こることがオーバーライドされる。
When the function object is assigned to a property, keep in mind that the method call syntax (target.func()) implicitly passes the target object as the first parameter. For YourClassName above, this would be the function object while a would be the target object. The expression used to retrieve the function object can be wrapped in parentheses to prevent this, as shown in the examples below.
呼び出されると、配列の各要素を順番に呼び出す。
class FuncArrayType extends Array {
Call(params*) {
; Call a list of functions.
for fn in this
fn(params*)
}
}
; Create an array of functions.
funcArray := FuncArrayType()
; Add some functions to the array (can be done at any point).
funcArray.Push(One)
funcArray.Push(Two)
; Create an object which uses the array as a method.
obj := {method: funcArray}
; Call the method (and consequently both One and Two).
obj.method("2nd")
; Call it as a function.
(obj.method)("1st", "2nd")
One(param1, param2) {
ListVars
MsgBox
}
Two(param1, param2) {
ListVars
MsgBox
}
関数のように動作するが、定義済みのパラメータを別の関数に渡すだけである。
BoundFuncオブジェクトを作成する方法は2つある:
BoundFuncオブジェクトは、以下の例のように呼び出すことができる。BoundFuncが呼び出されると、バインドされている関数やメソッドが呼び出され、バインドされているパラメータと呼び出し元のパラメータの組み合わせが渡される。未束縛のパラメータ位置は、呼び出し元のパラメータリストから左から右へと割り当てられる。事例:
fn := RealFn.Bind(1) ; Bind first parameter only
fn(2) ; 「1, 2」と表示します
fn.Call(3) ; 「1, 3」と表示します
fn := RealFn.Bind( , 1) ; Bind second parameter only
fn(2, 0) ; 「2, 1, 0」と表示します
fn.Call(3) ; 「3, 1」と表示します
fn(, 4) ; Error: 'a' was omitted
RealFn(a, b, c?) {
MsgBox a ", " b (IsSet(c) ? ", " c : "")
}
ObjBindMethodは、メソッド自体への参照を取得できない場合でも、メソッドにバインドするために使用できます。事例:
Shell := ComObject("Shell.Application")
RunBox := ObjBindMethod(Shell, "FileRun")
; Show the Run dialog.
RunBox
より複雑な例については、SetTimerを参照のこと。
その他のプロパティやメソッドはFuncから継承されるが、対象となる関数やメソッドのプロパティは反映されない(関数として実装する必要はない)。BoundFuncは、以下のファット・アロー関数と同様に、他の正式なパラメーターを持たない無名バリアド関数として機能する:
Func_Bind(fn, bound_args*) {
return (args*) => (args.InsertAt(1, bound_args*), fn(args*))
}