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.

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

A closure is a nested function which captured a variable from the scope that created it, so each evaluation of the enclosing function produces a distinct function object.

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. This is the distinction between obj.Method() and a plain function retrieved from the same property.

Params

Gets an Array describing the function's parameters. Keysharp extension.

Params := FuncObj.Params

Each element is an object with these properties:

The list excludes anything that cannot be supplied as an argument: the receiver of a method, and parameters internal to the implementation. On a bound function the already-bound parameters are excluded too, so Index numbers only the parameters that remain. The names are the same ones named-argument binding uses, so what this reports is exactly what binds.

The value is unset when the parameters cannot be known, which is the case for a reference produced by ObjBindMethod: it does not resolve its target until it is called. An empty Array is a real answer, meaning the function takes no arguments, so it cannot also stand for "no answer".

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

The underlying managed class is named FuncObj because .NET already defines a type named Func. Compatibility type checks recognize both Value is Func and Value is FuncObj.

Function objects can be obtained from a direct function reference or by passing a function name to Func(). Most built-in functions can also be used as function objects.

Calling a function object uses reflection and is slower than a direct function call. Prefer direct calls in performance-sensitive loops.