C3 AI Documentation Home

Overloading Methods

It is possible to overload methods when multiple method signatures are provided that differ in the arguments they accept. A method is overloaded if there are multiple definitions of the same method name with different sets of arguments.

Note that when overloading a method, everything in the method definition in the signature must be the same except for the set of arguments.

Definition of overloaded method

In the simplest case, a one method version can take in integers and return an integer while another version can take in doubles and return a double:

Type
type Math {

  min: function(args: double): double js-server

  min: function(args: Timeseries …): int js-server
}

It is also possible for a type to mixin another type with overloads and add additional overloads:

Type
type TimeseriesMath mixes Math {

  min: function(arg: double): double js-server
}

Note that the overloaded method should have the same implementation language and runtime as the original.

Implementation of overloaded methods

JavaScript and Python do not have a concept of overloads, but instead tend to examine the arguments at runtime to determine which signature is meant. This puts an extra burden on the implementer in these languages because if they override and overloaded method, they need to provide an implementation that covers all overloads.

Note that Type.super and Obj.super can help a lot with this.

In dynamic languages, there can only be a single function for one name, so both overloads of min should be claimed and implemented with a single function.

Example

Below is an example of a possible implementation for the method min defined in the example below. Note the handling of the overloads and timeseries.

JavaScript
function min(args) {

  if (args.length < 1)

    return;


  // handle numeric overloads

  if (typeof args[0] == 'number')

    return Math.min(args);


  // handle minimum of timeseries
   var ts = args[0];

for (var i = 1; i < args.length; i++)
     ts = ts.min(args[i]);
   return ts;
}
Was this page helpful?