Function CLASS

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

All Members


TypeNameSignature
method Sbindpublic static Function bind(Function, var)
method Sbindpublic static Function bind(Function, var, Any[])
methodgetFunctionKindpublic System.Reflection.FunctionKind getFunctionKind()
methodgetParameterspublic System.Reflection.Parameter[] getParameters()
methodgetReturnTypepublic System.Type getReturnType()
methodinvokepublic var invoke(Any[])

Methods


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

  • func The function to bind this and other arguments to.
  • thisObj The new this object. Must not be null.

Returns

  • A new function object that has new 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

  • func The function to bind this and other arguments to.
  • thisObj The new this object. Provide null to skip binding (therefore cannot bind null to it).
  • args The first N arguments to bind to this function's first N parameters. Provide an empty array or null to skip binding arguments.

Returns

  • A new function object that potentially has new this reference and a reduced parameter list.

public FunctionKind getFunctionKind()

Get the kind of this function.

Returns

  • An enum value representing the kind of the function.

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

  • An array of System.Parameter representing the parameters of this function.

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

  • An object representing the return type of the function.

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

  • args The same arguments that would be passed to the function when calling it normally, except the count of arguments doesn't have to be the same as that of parameters. The notation for this parameter's type should not be taked literately, as Julian doesn't support array type of Any.

Returns

  • The same value the function would return when being called normally, except if the function returns nothing (void), a null will be returned instead.