Function is the base class for all global functions, methods and lambdas defined in Julian.
Function has the first-class property in Julian. Like all other classes, it inherits Object. What sets it apart from other classes is the ability to execute. To invoke a function, use "()" grammar with argument expressions listed between ( and ). For example,fun(1, "a", someVal);
Since function is first-class instance, it can be assigned and passed around:void fun(){};
var f = fun;
f();
The same can be done for methods (both instance and static) and lambdas.
In general, Function in Julian doesn't enforce a strong typing. When a Function type is declared somewhere, a function instance of any given signature can be passed in. By the time a function is invoked, the arguments will be checked strickly, and when the function returns, the return value's type will also be checked against the declared type. This behavior can be relaxed a little by dynamic invocation.
This class is neither instantiable nor extensible. For more detailed description on Function, see tutorial on Function.
Parent Class
Type | Name | Signature |
---|---|---|
method S | bind | public static Function bind(Function, var) |
method S | bind | public static Function bind(Function, var, Any[]) |
method | getFunctionKind | public System.Reflection.FunctionKind getFunctionKind() |
method | getParameters | public System.Reflection.Parameter[] getParameters() |
method | getReturnType | public System.Type getReturnType() |
method | invoke | public var invoke(Any[]) |
public static Function bind(Function func, var thisObj)
Bind the function with a new reference to this
to create a new function object.
The sole argument is the new this
object to be referenced from inside the resultant function. Unlike the overloaded version of this method, it must be not null.
The new function will always have a this
reference, even if the current one doesn't have a this
reference:void foo() { this.bar(); }
foo.bind(new Bar());
foo(); // If we didn't bind this would throw.
Not all Function objects can be bound, and not binding of arbitrary this
object is allowed. As of 0.1.34, this method cannot be called against hosted functions and method groups.
Even if the Function is bindable, this method may fail and throw for a varierty of reasons. For example, it's not allowd to bind a this
object to an instance method where the type of new this
object differs from the defined one.
Parameters
this
and other arguments to.
this
object. Must not be null.Returns
this
reference.public static Function bind(Function func, var thisObj, var[] args)
Bind the function with this
object and new arguments to create a new function object.
The first argument is the new this
object to be referenced from inside the resultant function. The second argument, an array, can contain arbitrary number of arguments in an order corresponding to the current function's argument list, excluding the implicit this
argument. Both of the arguments are optional.
The function returned is a completely new one that is independent from the current function.
The new function may have a new this
reference, even if the current one doesn't have a this
reference:void foo() { this.bar(); }
foo.bind(new Bar(), null);
foo(); // If we didn't bind this would throw.
The new function may have a reduced list of arguments. Calling it, therefore, may require less arguments, as illustrated by this example:void fun(int i, string s, char c) { ... }
var newFun = fun.bind(null, new var[]{10});
newFun("hello", 'x'); // Notice that we don't provide the integer argument anymore.
Not all Function objects can be bound, and not binding of arbitrary this
object is allowed. As of 0.1.34, this method cannot be called against hosted functions and method groups.
Even if the Function is bindable, this method may fail and throw for a varierty of reasons, including: binding a this
object to an instance method where the type of new this
object differs from the defined one; providing arguments that do not align with the defined parameter list in terms of the compatibility.
Parameters
this
and other arguments to.
this
object. Provide null
to skip binding (therefore cannot bind null
to it).
null
to skip binding arguments.Returns
this
reference and a reduced parameter list.public FunctionKind getFunctionKind()
Get the kind of this function.
Returns
public Parameter[] getParameters()
Get the parameters of this function.
If it's an instance method, the resultant array would contain as the first parameter the type of the instance's class, with the name of 'this'. If it's an extension method, the first element will be of the extended class, also with the name of 'this'. This applies no matter how the function object is obtained (either from an extended intance, such as Function f = ext.exfun;
, or from the extension class directly, i.e. Function f = ExtClass.exfun;
).
Since a method group contains multiple function objects differentiated exactly by the parameter list, this method simply returns those for the first function. The caller is therefore advised to call getFunctionKind() to understand what the returned value means.
Returns
public Type getReturnType()
Get the return type for this function.
A lambda doesn't have an explicitly defined return type. So this method would return null.
Returns
public var invoke(var[] args)
Invoke the function dynamically.
When calling a function using Julian's built-in grammar, namely in the form of "fun(a, b)", the arguments are checked for each parameter's type at corresponding position, and the count of arguments are also checked against that of parameters.
When invoking a function dynamically, however, the count of arguments are not checked. If the arguments are less than required, the missing ones will be initialized with the default value of the declared parameeter type. In the contrast, if the arguments are more than required, the excessive one will be discarded. For the arguments that fall into the range of paramters, their types are still checked as in the normal calling procedure.
Another property of dynamic invocation is that if the function doesn't return a value (void), a null value will be returned instead.
Parameters
Returns