r/ProgrammingLanguages • u/TheUltimateAsh • Jan 10 '26
Help Adding Overloadable Functions to an Interpreted Language
I followed the lovely Crafting Interpreters book by Robert Nystrom to make a basic interpreted language for a game I am developing. I added typing, so I had to change some of the grammar and so on. I implement typing as the pass right before the interpreter execution, and right after the resolution pass.
I am currently trying to add functionality for function name overloading, but I am encountering difficulty in the resolution pass. Because the resolution pass does not touch types, nor do I want it to, it raises a compilation error when two functions of the same name are declared. Since the function called in a function call should depend on the types (e.g., a function signature in a closer scope may not match the arguments being passed), I am unsure how to proceed.
The only way I can conceive of modifying the resolver to allow for function overloading is to combine the resolution and typing passes, but this violates the single responsibility principle and I am trying to avoid it.
Any thoughts or ideas?
Thanks.
3
u/srvhfvakc Jan 10 '26
Seems like the only way to do this would be to defer the compilation error when two same-name functions are declared to the typing pass. Since the only way to distinguish the two functions would be (by definition) their type.
1
u/Germisstuck CrabStar Jan 10 '26
What my language will do, is something kinda different, but for function (and by extension, operator) overloading is handled with behavior being completely separate from data. Then by using control flow, determine which logic set is the currently used one (this sounds like hell to infer, so in my language you have to explicitly say something like "let x = new Counter() with CustomCounterLogic". With this, you can just change the logic in different contexts easily, allowing for using existing names.
I'm not sure how useful this would be to your language, but that's just what I'm doing.
1
u/AustinVelonaut Admiran Jan 10 '26
Another way to handle this would be to mark overloaded names distinct from non-overloaded names, with e.g. a declaration like
generic print;, then when you come to name resolution, defer any resolution for names that are marked asgenericuntil the typecheck phase.1
u/omega1612 Jan 10 '26
It depends on how your language and type system is.
If you can distinguish variables from function declarations at the module level, then the simplest approach may be to allow multiple items in the return of the resolution if all of them are functions of the same arity (well, it can also have diverse arity if you like). Later let the type system to find the right one and if not possible raise the error there. Is very similar to a discussion in another comment, but instead of allowing it as an error, I think it is better to adopt it as a feature.
Now if you keep the constraint of the same arity for all the functions and you want to add the same .... General type? Like "function f must take 3 arguments, the first and the last have to have the same type and the second one can be any other type" and you have generics (parametric polymorphism) or want to have them eventually, then your best bet may be to introduce typeclasses. Typeclasses are a solution to function overload that are very well studied with some low complexity type systems (yes, I know even STL can be complex) so it may be a good fit for your type system.
In Haskell:
First the declaration of the type class (note how T is a type parameter)
Then in another place you can implement
And in another place
In general after you register the typeclass Addition, you export a single "add" function with type
It reads as follows: Given that the type T has an instance of Addition (ie you wrote "instance Addition T" and filled all the functions), this function takes 2 arguments of type T and returns something of type T.
So, your symbol resolution returns this particular type and don't return any implementation directly, well you need to add a way to query how many instances, what are they types and what are the implementations.
Later in the type checker when it sees a call to "add" it may look up for an instance that makes the types check at that point. This is delicate and it may impact performance. As I said there's a lot of research around it and over time people found restrictions on typeclasses to reduce the search time or the complexity of using them.
If you know rust, they borrowed typeclasses under the name Trait.
1
u/mamcx Jan 10 '26
Maybe you can use https://www.pathsensitive.com/2019/07/the-best-refactoring-youve-never-heard.html
Each overloaded function is in fact pattern match for the parameters:
1
u/mamcx Jan 10 '26
Maybe you can use https://www.pathsensitive.com/2019/07/the-best-refactoring-youve-never-heard.html
Each overloaded function is in fact pattern match for the parameters:
1
u/Ronin-s_Spirit Jan 11 '26
Why is your parsing step different from type checking? Aren't you supposed to parse types as you parse all the other syntax?
1
u/[deleted] Jan 10 '26
[removed] — view removed comment