Overloading
If multiple functions have the same name, they are called overloaded.
They are still just ordinary functions with no special relationship other than having a name in common.
When Keen sees a function call, it creates a list of all functions in scope with that name.
It then filters them by:
- The number of arguments
- The return type
- Argument types
After that, if there are multiple functions remaining, it is an error due to ambiguity.
Number of arguments
Overloading by number of arguments is simple:
Return type
Overloading by return type uses the expected type of the expression.
The expected type could also come from the argument type of an outer function.
Below, since bar takes a string, that overload of foo is used.
Argument type
Basic overloading by argument type:
Troubleshooting
If you are having trouble with a piece of code and want to be sure you are calling the intended overload,
add type annotations on everything, as in:
foo::string bar::symbol
That way it's clear you want to call foo string(_ symbol) .
Once the code is working, remove unnecessary type annotations.
More details of Keen type-checking are in Advanced overloading.