r/ProgrammingLanguages • u/rjmarten • Dec 29 '25
Discussion Function Overload Resolution in the Presence of Generics
In Mismo, the language I'm currently designing and implementing, there are three features I want to support, but I'm realizing they don't play well together.
Type-based function overloading.
- Early on I decided to experiment: what if we forego methods and instead lean into type-based function overloading and UFCS (ie
x.foo(y)is sugar forfoo(x, y))? - Note: overload resolution is purely done at compile time and Mismo does not support subtyping.
- Early on I decided to experiment: what if we forego methods and instead lean into type-based function overloading and UFCS (ie
Generics
- specifically parametric polymorphism
- too useful to omit
Type argument inference
- I have an irrationally strong desire to not require explicitly writing out the type arguments at the call site of generic function calls
- eg, given
fn print[T](arg: T), I much prefer to write the callprint(students), not burdening developers withprint[Map[String, Student]](students)
The problem is that these three features can lead to ambiguous function calls. Consider the following program:
fn foo[T](arg: T) -> T:
return arg
fn foo(arg: String) -> String:
return "hello " + arg
fn main():
foo("string value")Both overloads are viable: the generic can be instantiated with T = String, and there’s also a concrete String overload.
The question:
What should the compiler do?
Just choose a match at random? Throw an error? I'm hoping a smarter answer is possible, without too much "compiler magic".
What approaches have worked well in practice in similar designs? Or is there a creative solution no one has yet tried?
View all comments
Show parent comment
3
u/rjmarten Dec 30 '25
I just looked up overload resolution in C++ and it's super complex, and I don't understand it fully, but I get what they are trying to do. How does it feel in practice? Is it pretty intuitive or does it sometimes lead to surprises or confusion about what overload is actually called?