Func Object

class Func extends Object

Represents a user-defined or built-in function and provides an interface to call it, bind parameters to it, and retrieve information about it or its parameters.

For information about other objects which can be called like functions, see Function Objects.

The Closure class extends Func but does not define any new properties.

For each built-in function or function definition within the script, there is a corresponding read-only variable containing a Func object. This variable is directly used to call the function, but its value can also be read to retrieve the function itself, as a value. For example:

InspectFn StrLen
InspectFn InspectFn

InspectFn(fn)
{
    ; Display information about the passed function.
    MsgBox fn.Name "() is " (fn.IsBuiltIn ? "built-in." : "user-defined.")
}

"FuncObj" is used below as a placeholder for any Func object, as "Func" is the class itself.

In addition to the methods and properties inherited from Object, Func objects have the following predefined methods and properties.

IsClosure, IsMethod and Params are Keysharp extensions.

Table of Contents

Methods

Call

Calls the function.

FuncObj(Param1, Param2, ...)
FuncObj.Call(Param1, Param2, ...)

Parameters

Param1, Param2, ...

Parameters and return value are defined by the function.

Remarks

The "Call" method is implied when calling a value, so need not be explicitly specified.

Bind

Binds parameters to the function.

BoundFunc := FuncObj.Bind(Param1, Param2, ...)

Parameters

Param1, Param2, ...

Any number of parameters.

Return Value

Type: Object

This method returns a BoundFunc object.

IsByRef

Determines whether a parameter is ByRef.

Boolean := FuncObj.IsByRef(ParamIndex)

Parameters

ParamIndex

Type: Integer

If omitted, Boolean indicates whether the function has any ByRef parameters. Otherwise, specify the one-based index of a parameter.

Return Value

Type: Integer (boolean)

This method returns 1 (true) if the parameter is ByRef, otherwise 0 (false). If ParamIndex is invalid, an exception is thrown.

IsOptional

Determines whether a parameter is optional.

Boolean := FuncObj.IsOptional(ParamIndex)

Parameters

ParamIndex

Type: Integer

If omitted, Boolean indicates whether the function has any optional parameters. Otherwise, specify the one-based index of a parameter.

Return Value

Type: Integer (boolean)

This method returns 1 (true) if the parameter is optional, otherwise 0 (false). If ParamIndex is invalid, an exception is thrown.

Remarks

Parameters do not need to be formally declared if the function is variadic. Built-in functions are supported.

Properties

Name

Gets the function's name.

FunctionName := FuncObj.Name

IsBuiltIn

Gets 1 (true) if the function is built-in, otherwise 0 (false).

Boolean := FuncObj.IsBuiltIn

IsVariadic

Gets 1 (true) if the function is variadic, otherwise 0 (false).

Boolean := FuncObj.IsVariadic

IsClosure

Gets 1 (true) if the function is a closure, otherwise 0 (false).

Boolean := FuncObj.IsClosure

IsMethod

Gets 1 (true) if the function is a method, otherwise 0 (false).

Boolean := FuncObj.IsMethod

A method receives the target object as a hidden first parameter, this, so calling it requires a target.

Params

Gets an Array describing the function's parameters.

Params := FuncObj.Params

Each element is an object with these properties:

The list holds what a caller supplies. A method with no target attached, such as one read from a prototype, lists its receiver first as this; parameters internal to the implementation are left out. On a bound function the already-bound parameters are excluded too, so Index numbers only the parameters that remain.

The value is unset when the parameters cannot be known, such as for a function produced by ObjBindMethod, which does not resolve its target until it is called.

MyFunc(Alpha, Beta := 5, &Out?, Rest*) => Alpha
for p in MyFunc.Params
    MsgBox p.Index ": " p.Name (p.HasOwnProp("Default") ? " = " p.Default : "")

MinParams

Gets the number of required parameters.

ParamCount := FuncObj.MinParams

MaxParams

Gets the number of formally-declared parameters for a user-defined function or maximum parameters for a built-in function.

ParamCount := FuncObj.MaxParams

If the function is variadic, ParamCount indicates the maximum number of parameters which can be accepted by the function without overflowing into the "variadic*" parameter.

Implementation Remarks

Calling a function object is slower than a direct function call.