Function Objects

"Function object" usually means any of the following:

Function objects can be used with the following:


User-defined function objects must define a Call method containing the implementation of the "function".

class YourClassName {
    Call(a, b) {  ; Declare parameters as needed, or an array*.
        return c


The following example defines a function array which can be called; when called, it calls each element of the array in turn.

class FuncArrayType {
    Call(obj, params*) {
        ; Call a list of functions.
        Loop this.Length()

; Create an array of functions.
funcArray := new FuncArrayType
; Add some functions to the array (can be done at any point).
; Create an object which uses the array as a method.
obj := {method: funcArray}
; Call the method.
obj.method("foo", "bar")

One(param1, param2) {
Two(param1, param2) {

BoundFunc Object

Acts like a function, but just passes predefined parameters to another function.

There are two ways that BoundFunc objects can be created:

BoundFunc objects can be called as shown in the example below. When the BoundFunc is called, it calls the function or method to which it is bound, passing any bound parameters followed by any which were passed by the caller. For example:

fn := Func("RealFn").Bind(1)

%fn%(2)    ; Shows "1, 2"
fn.Call(3) ; Shows "1, 3"

RealFn(a, b) {
    MsgBox a ", " b

ObjBindMethod can be used to bind to a method when it isn't possible to retrieve a reference to the method itself. For example:

file := FileOpen(A_ScriptFullPath, "r")
getLine := ObjBindMethod(file, "ReadLine")
MsgBox %getLine%()  ; Shows the first line of this file.

For a more complex example, see SetTimer.

Other properties and methods are inherited from Func, but do not reflect the properties of the target function or method (which is not required to be implemented as a function). The BoundFunc acts as an anonymous variadic function with no other formal parameters, similar to the fat arrow function below:

Func_Bind(func, bound_args*) {
    return (args*) => (args.InsertAt(1, bound_args*), %func%(args*))