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:

main void() log foo 1 log foo 1, 2 foo nat(a nat) a * 10 foo nat(a nat, b nat) a + b

Return type

Overloading by return type uses the expected type of the expression.

main void() n nat = foo s string = foo # This would be an error due to ambiguity # x = foo log n, s foo nat() 1 foo string() "a"

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.

main void() log bar foo bar string(a string) "foo {a}" foo nat() # not used 1 foo string() "a"

Argument type

Basic overloading by argument type:

main void() log foo 1 log foo "a" foo nat(a nat) a * 10 foo string(a string) a

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.