Overriding Methods
The C3 Agentic AI Platform provides the ability to override methods.
The following sections detail how to override a method.
Re-implementation
The simplest form of an override is a re-implementation of the same method, which is simply denoted with ~ syntax used for any field type. This syntax indicates no change in the signature of the method (arguments, return type, implementation language, and runtime are all the same) but only that this type is going to provide a new implementation of the method.
Note that if you want an overridden method to have a different runtime or implementation language, you may do so by including the desired specifications in the method definition.
/**
* Mean.c3typ
*/
type Mean {
mean: function(values: ![double]): double js-server
}
/**
* ArithmeticMean.c3typ
*/
type ArithmeticMean mixes Mean {
//re-implementation
mean: ~
//re-implementation in a different language and runtime
mean: ~ : double py client
}More specific return value
If the developer wants to make a return value of a method more specific in a sub-type, this can be done without repeating signature using the ~ syntax. In the example below, the method mean is overridden to return a "!double" value.
/**
* Mean.c3typ
*/
type Mean {
mean: function(values: ![double]): double js-server
}
/**
* ArithmeticMean.c3typ
*/
type ArithmeticMean mixes Mean {
mean: ~ : !double
}Best practices and rules
Here are some general rules to follow when overriding methods:
- If a type mixes-in a type that defines a method, it may not re-define that method with the same parameters.
- A type must use override syntax to indicate that it is overriding a method of another type it mixes-in.
- If the method is re-defined with different parameters, that constitutes an overload.
- A type that mixes in an abstract type is assumed to implement all abstract methods, so it only needs to re-declare them abstract if they will not be implemented.
- An override cannot change a method from public to private but the opposite is possible.
- An override cannot change a method from static to member or vice versa.
- If you do not use override syntax and declare a method with different parameters, you are overloading.