|
|
---
|
|
|
tags:
|
|
|
- errors
|
|
|
- errors/overloaded-functions
|
|
|
---
|
|
|
|
|
|
In sac, you can [overload](/concepts/overloading.md) functions.
|
|
|
This is great but the errors relating to this feature can be improved a lot.
|
|
|
|
|
|
## Show available options if no type match is found
|
|
|
|
|
|
If you have a program like this:
|
|
|
|
|
|
```c
|
|
|
use StdIO: all;
|
|
|
use Array: {!};
|
|
|
|
|
|
void foo(int a) { return;}
|
|
|
void foo(int[3] a) { return; }
|
|
|
bool foo(bool a) { return !a; }
|
|
|
|
|
|
int main() {
|
|
|
foo([2,3]);
|
|
|
return 0;
|
|
|
}
|
|
|
```
|
|
|
|
|
|
The current error is:
|
|
|
```
|
|
|
All instances of "main" contain type errors
|
|
|
No matching definition found for the application of "_MAIN::foo" to arguments ( int[2]{2,3})
|
|
|
Compilation failed while Running type inference system, 1 error(s), 1 warning(s).
|
|
|
|
|
|
```
|
|
|
|
|
|
Instead, we could have:
|
|
|
|
|
|
```
|
|
|
All instances of "main" contain type errors
|
|
|
No matching definition found for the application of foo to arguments of type (int[2]{2,3})
|
|
|
|
|
|
Here are the versions of foo I know about:
|
|
|
|
|
|
int -> void
|
|
|
int[3] -> void
|
|
|
bool -> bool
|
|
|
```
|
|
|
|
|
|
Notice the formatting of the types.
|
|
|
The arrows are alligned to make it more readable.
|
|
|
Sijmen did an internship about this.
|
|
|
## Global objects are printed
|
|
|
|
|
|
If you have code like this:
|
|
|
|
|
|
```c
|
|
|
use StdIO: all;
|
|
|
|
|
|
void foo(int a) { printf("Called foo with int\n"); return;}
|
|
|
|
|
|
int main() {
|
|
|
foo(true);
|
|
|
return 0;
|
|
|
}
|
|
|
```
|
|
|
|
|
|
Currently, the compiler prints the global objects that are woven true in the overloading error message.
|
|
|
|
|
|
```
|
|
|
No definition found for a function "_MAIN::foo" that accepts an argument of type "bool{1}" as parameter no 3. Full argument types are "( Terminal::Terminal, TermFile::TermFile, bool{1})".
|
|
|
Compilation failed while Running type inference system, 1 warning(s).
|
|
|
```
|
|
|
|
|
|
I think that this can be very confusing for the programmer.
|
|
|
Especially because the parameter number does not match up with the parameter number in their source code.
|
|
|
|
|
|
## Not assigning a function to a result overloads to void return types is unclear
|
|
|
|
|
|
This is a very cool feature. If you have code like this:
|
|
|
|
|
|
```c
|
|
|
use Array: all;
|
|
|
use StdIO: all;
|
|
|
|
|
|
void foo() { printf("void\n"); return;}
|
|
|
int foo() { return 137;}
|
|
|
|
|
|
int main() {
|
|
|
foo();
|
|
|
b = foo();
|
|
|
print(b);
|
|
|
|
|
|
return 0; }
|
|
|
```
|
|
|
|
|
|
You will get:
|
|
|
|
|
|
```
|
|
|
Not assigned, got 3
|
|
|
Assigned!
|
|
|
```
|
|
|
|
|
|
If you call an overloaded function without assigning the result to a variable the compiler will pick the version that returns void.
|
|
|
|
|
|
However, the error message about this is not very clear. Let's say you have:
|
|
|
|
|
|
```c
|
|
|
int ten() { return 10;}
|
|
|
void main() { ten(); }
|
|
|
```
|
|
|
|
|
|
You get:
|
|
|
|
|
|
```
|
|
|
./test.sac:3:15: abort:
|
|
|
No definition found for a function "_MAIN::ten" that expects 0 argument(s) and yields 0 return value(s)
|
|
|
Compilation failed while Converting to static single assignment form, 1 warning(s).
|
|
|
```
|
|
|
|
|
|
I found this very unclear when I started out with sac.
|
|
|
I was used to programming languages just dropping the return value.
|
|
|
We should say something about this in the error message:
|
|
|
|
|
|
```
|
|
|
No matching definition found for the application of foo to arguments of type (int[2]{2,3})
|
|
|
|
|
|
Here are the versions of foo I know about:
|
|
|
|
|
|
int
|
|
|
|
|
|
**Hint: You did not assing the result of foo() to a variable. This will cause the overloading to look for a version of foo that returns void but this version does not exist.
|
|
|
|
|
|
Try to assign the result of foo() to a variable or add a version that returns void.
|
|
|
```
|
|
|
|
|
|
This also has weird error if you run the example above with printing in both functions:
|
|
|
|
|
|
```c
|
|
|
use Array: all;
|
|
|
use StdIO: all;
|
|
|
|
|
|
void foo() { printf("void\n"); return;}
|
|
|
// Works without the printf.
|
|
|
int foo() { printf("Bug"); return 137;}
|
|
|
|
|
|
int main() {
|
|
|
foo();
|
|
|
b = foo();
|
|
|
print(b);
|
|
|
|
|
|
return 0; }
|
|
|
```
|
|
|
|
|
|
# Arity 0
|
|
|
|
|
|
If you try to overload functions that have no arguments like this
|
|
|
|
|
|
```c
|
|
|
bool foo() { return true;}
|
|
|
int foo() { return 137;}
|
|
|
|
|
|
int main() { return 0; }
|
|
|
```
|
|
|
You get this error message.
|
|
|
`
|
|
|
```
|
|
|
Cannot overload functions of arity 0
|
|
|
```
|
|
|
|
|
|
This makes sense. But here we could add why we can't and what arity is, where the functions with 0 arity are. |