FunctionOverloading

From dis-Emi-A

Jump to: navigation, search


In this language we do not have a distinction between functions and variables, such that the name lookup for functions is the same as it is for variables. In traditional thinking where a variable has only one possible current value, this would seem to make function overloading very difficult.

Basics

Using the Fibonacci sequence as an example, we see we need this definition:

fibonacci = at -> fibonacci( at - 1 ) + fibonacci( at - 2 );

fibonacci = 1 -> 1

fibonacci = 0 -> 1

If we stick with the equals operator then the fibonacci has only the single function 0 -> 1, which means it is broken. Therefore we need some sort of special syntax to allow overloading.

fibonacci :> at -> fibonacci( at - 1 ) + fibonacci( at - 2 );

fibonacci :> 1 -> 1

fibonacci :> 0 -> 1

What this really does is create a function table variable fibonacci which contains three different function definitions. Upon using the fibonacci variable in a function call the appropriate signature and constraint will be matched to call the correct function.

The variable is truly a real variable though, and indeed it is a set, so you could do things like iterator over it:

Describe([ fibonacci ]);

Providing there is a describe function which can show something about the signatures (some kind of runtime typing) this would print the description for all of the fibonacci definitions.


Namespaces

Introducing namespaces becomes quite difficult, for surely overloading has to work across namespace boundaries when implicitly included, but also automatically when not included for auto-typing. Refer to the discussion on Namespace AutoTyping.

This requires that all function assign expressions end up modifying a single variable of that name, and the lookup procedure needs to determine namespace applicability.

In pseudo-code

namespace nsName
{

OP( :> ) = funcName, defn
{
  @OP( :> )( ( nsName ), funcName, defn );
}

OP( :> ) = nsSet, funcName, defn
{
  @OP( :> )( nsName ++ nsSet, funcName, defn );
}

}

...?

Personal tools